diff --git a/src/api/createtransformer.ts b/src/api/createtransformer.ts index 92424df12..c3485ed81 100644 --- a/src/api/createtransformer.ts +++ b/src/api/createtransformer.ts @@ -45,12 +45,14 @@ export function createTransformer(transformer: ITransformer, onClean } function getMemoizationId(object) { + if(typeof object === 'string' || typeof object === 'number') + return object if (object === null || typeof object !== "object") - throw new Error("[mobx] transform expected some kind of object, got: " + object); + throw new Error("[mobx] transform expected some kind of object or primitive value, got: " + object); let tid = object.$transformId; if (tid === undefined) { tid = getNextId(); addHiddenProp(object, "$transformId", tid); } return tid; -} \ No newline at end of file +} diff --git a/test/transform.js b/test/transform.js index 115127ed4..9292c8946 100644 --- a/test/transform.js +++ b/test/transform.js @@ -1003,4 +1003,53 @@ test('transform tree (dynamic tags - peek / rebuild)', function(t) { t.deepEqual(state.renderedNodes.length, 0); t.end(); -}); \ No newline at end of file +}); + +// https://github.com/mobxjs/mobx/issues/886 +test('transform with primitive key', function(t) { + m.extras.resetGlobalState(); + + function Bob() { + this.num = Math.floor(Math.random() * 1000); + m.extendObservable(this, { + get name() { + return 'Bob' + this.num; + } + }); + } + + var observableBobs = m.observable([]); + var bobs = []; + + var bobFactory = m.createTransformer(function(key) { + return new Bob(); + }); + + m.autorun(function() { + bobs = observableBobs.map(function(bob) { + return bobFactory(bob); + }); + }); + + observableBobs.push('Bob1'); + observableBobs.push('Bob1'); + t.equal(bobs[0].name, bobs[1].name); + + observableBobs.clear(); + observableBobs.push('Bob1'); + observableBobs.push('Bob2'); + t.notEqual(bobs[0].name, bobs[1].name); + + observableBobs.clear(); + observableBobs.push(1); + observableBobs.push(1); + t.equal(bobs[0].name, bobs[1].name); + + observableBobs.clear(); + observableBobs.push(1); + observableBobs.push(2); + t.notEqual(bobs[0].name, bobs[1].name); + + t.end(); +}); +