From e0248ce03acf77f9f092e2a22f48c6952203ddda Mon Sep 17 00:00:00 2001 From: Zhongliang Wang Date: Mon, 12 Jun 2017 04:10:27 +0800 Subject: [PATCH] Remove lodash (#502) --- .travis.yml | 2 +- addon/adapters/firebase.js | 55 +++++++++++++------------ addon/initializers/emberfire.js | 17 ++++---- addon/serializers/firebase.js | 3 +- package.json | 2 - tests/unit/serializers/firebase-test.js | 3 +- vendor/legacy/header.js | 10 ----- 7 files changed, 42 insertions(+), 50 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1b17a792..ed29cc6e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -48,7 +48,7 @@ install: script: - npm run legacy - - ember try:one $EMBER_TRY_SCENARIO --skip-cleanup + - ember try:one $EMBER_TRY_SCENARIO after_script: - if [ "$COVERAGE" ]; then sed 's/SF:modules\/emberfire\//SF:addon\//g' coverage/lcov.info > coverage/lcov.info.fixed; fi diff --git a/addon/adapters/firebase.js b/addon/adapters/firebase.js index 754ad3f9..a8030cf8 100644 --- a/addon/adapters/firebase.js +++ b/addon/adapters/firebase.js @@ -2,21 +2,14 @@ import Ember from 'ember'; import DS from 'ember-data'; import Waitable from '../mixins/waitable'; import toPromise from '../utils/to-promise'; -import assign from 'lodash/object/assign'; -import forEach from 'lodash/collection/forEach'; -import filter from 'lodash/collection/filter'; -import map from 'lodash/collection/map'; -import includes from 'lodash/collection/includes'; -import indexOf from 'lodash/array/indexOf'; -import find from 'lodash/collection/find'; -var Promise = Ember.RSVP.Promise; +const { assign, RSVP: { Promise } } = Ember; var uniq = function (arr) { var ret = Ember.A(); arr.forEach(function(k) { - if (indexOf(ret, k) < 0) { + if (ret.indexOf(k) < 0) { ret.push(k); } }); @@ -521,26 +514,26 @@ export default DS.Adapter.extend(Waitable, { var dirtyRecords = []; // Added - var addedRecords = filter(ids, (id) => { + var addedRecords = ids.filter((id) => { return !idsCache.includes(id); }); // Dirty - dirtyRecords = filter(ids, (id) => { + dirtyRecords = ids.filter((id) => { var relatedModelName = relationship.type; return store.hasRecordForId(relatedModelName, id) && store.peekRecord(relatedModelName, id).get('hasDirtyAttributes') === true; }); - dirtyRecords = map(uniq(dirtyRecords.concat(addedRecords)), (id) => { + dirtyRecords = uniq(dirtyRecords.concat(addedRecords)).map((id) => { return this._saveHasManyRecord(store, typeClass, relationship, recordRef, id); }); // Removed - var removedRecords = filter(idsCache, (id) => { - return !includes(ids, id); + var removedRecords = idsCache.filter((id) => { + return !ids.includes(id); }); - removedRecords = map(removedRecords, (id) => { + removedRecords = removedRecords.map((id) => { return this._removeHasManyRecord(store, recordRef, relationship.key, typeClass, id); }); // Combine all the saved records @@ -704,23 +697,30 @@ export default DS.Adapter.extend(Waitable, { * @return {Object} */ getFirstEmbeddingParent(internalModel) { - var relationships = assign( + let relationships = assign( {}, internalModel._implicitRelationships, internalModel._relationships.initializedRelationships ); - var embeddingParentRel = find(relationships, (rel) => { - var members = rel.members.toArray(); - var parent = members[0]; + let embeddingParentRel; + let relationshipKeys = Object.keys(relationships); + + for (let i = 0; i < relationshipKeys.length; i++) { + let rel = relationships[relationshipKeys[i]]; + let members = rel.members.toArray(); + let parent = members[0]; if (!parent || !rel.inverseKey) { - return false; + continue; } - var parentRel = parent._relationships.get(rel.inverseKey); - return this.isRelationshipEmbedded(this.store, parent.type.modelName, parentRel.relationshipMeta); - }); + let parentRel = parent._relationships.get(rel.inverseKey); + if (this.isRelationshipEmbedded(this.store, parent.type.modelName, parentRel.relationshipMeta)) { + embeddingParentRel = rel; + break; + } + } if (embeddingParentRel) { var parent = embeddingParentRel.members.toArray()[0]; @@ -766,7 +766,7 @@ export default DS.Adapter.extend(Waitable, { return; } - forEach(this._queue, (key) => { + this._queue.forEach((key) => { const { payload, modelName } = this._queuedPayloads[key]; const normalizedData = store.normalize(modelName, payload); store.push(normalizedData); @@ -797,7 +797,7 @@ export default DS.Adapter.extend(Waitable, { const key = `${modelName}-${id}`; if (this._queuedPayloads[key]) { // remove from original place in queue (will be added to end) - const oldPosition = indexOf(this._queue, key); + const oldPosition = this._queue.indexOf(key); this._queue.splice(oldPosition, 1); } this._queuedPayloads[key] = { payload, modelName }; @@ -836,10 +836,11 @@ export default DS.Adapter.extend(Waitable, { const isEmbedded = this.isRelationshipEmbedded(store, typeClass.modelName, relationship); if (isEmbedded) { const relationshipTypeClass = store.modelFor(relationship.type); - forEach(relationshipPayload, (obj, id) => { + for (let id in relationshipPayload) { + let obj = relationshipPayload[id]; obj.id = id; this._updateRecordCacheForType(relationshipTypeClass, obj, store); - }); + } } else { const ids = Object.keys(relationshipPayload); cache[key] = Ember.A(ids); diff --git a/addon/initializers/emberfire.js b/addon/initializers/emberfire.js index 453915d3..450553d9 100644 --- a/addon/initializers/emberfire.js +++ b/addon/initializers/emberfire.js @@ -3,7 +3,6 @@ import DS from 'ember-data'; import firebase from 'firebase'; import FirebaseAdapter from '../adapters/firebase'; import FirebaseSerializer from '../serializers/firebase'; -import forEach from 'lodash/collection/forEach'; var VERSION = '0.0.0'; @@ -43,13 +42,15 @@ export default { _emberfirePatched: true, _emberfireHandleRecordPush(records) { - forEach(records, (record) => { - var modelName = record.constructor.modelName; - var adapter = this.adapterFor(modelName); - if (adapter.recordWasPushed) { - adapter.recordWasPushed(this, modelName, record); - } - }); + if (typeof records !== 'undefined') { + records.forEach((record) => { + var modelName = record.constructor.modelName; + var adapter = this.adapterFor(modelName); + if (adapter.recordWasPushed) { + adapter.recordWasPushed(this, modelName, record); + } + }); + } }, push() { diff --git a/addon/serializers/firebase.js b/addon/serializers/firebase.js index 38ddfcde..ef1fdef4 100644 --- a/addon/serializers/firebase.js +++ b/addon/serializers/firebase.js @@ -1,8 +1,9 @@ import Ember from 'ember'; import DS from 'ember-data'; -import assign from 'lodash/object/assign'; import firebase from 'firebase'; +const { assign } = Ember; + /** * The Firebase serializer helps normalize relationships and can be extended on * a per model basis. diff --git a/package.json b/package.json index 1d1df011..029ccb57 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,6 @@ "broccoli-webpack": "^1.0.0", "chalk": "1.1.3", "ember-cli-babel": "5.1.6", - "ember-lodash": "0.0.7", "firebase": "^3.4.1" }, "devDependencies": { @@ -91,7 +90,6 @@ "gulp-sourcemaps": "1.6.0", "gulp-uglify": "1.5.3", "gulp-util": "3.0.7", - "lodash": "3.10.1", "sinon": "^1.17.3", "torii": "0.8.0", "vinyl-buffer": "1.0.0", diff --git a/tests/unit/serializers/firebase-test.js b/tests/unit/serializers/firebase-test.js index 20b2208d..62448e77 100644 --- a/tests/unit/serializers/firebase-test.js +++ b/tests/unit/serializers/firebase-test.js @@ -9,7 +9,8 @@ import Post from 'dummy/models/post'; import WithDate from 'dummy/models/with-date'; import Comment from 'dummy/models/comment'; import User from 'dummy/models/user'; -import assign from 'lodash/object/assign'; + +const { assign } = Ember; describe('FirebaseSerializer', function() { setupTest('emberfire@serializer:firebase', { diff --git a/vendor/legacy/header.js b/vendor/legacy/header.js index 8cf1d87d..b11ee660 100644 --- a/vendor/legacy/header.js +++ b/vendor/legacy/header.js @@ -7,13 +7,3 @@ * https://github.com/firebase/emberfire/ * License: MIT */ - - /** - * @license - * lodash 3.10.0 (Custom Build) - * Build: `lodash modularize modern exports="es" -o ./` - * Copyright 2012-2015 The Dojo Foundation - * Based on Underscore.js 1.8.3 - * Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors - * Available under MIT license - */