diff --git a/index.js b/index.js index 7d3fc30..62a23a4 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,7 @@ module.exports = class ExpPipelineMap extends Promise { return this.then((object) => { const targetObject = {}; - properties.filter((property) => object.hasOwnProperty(property)) + properties.sort().filter((property) => object.hasOwnProperty(property)) .forEach((property) => { targetObject[property] = object[property]; }); diff --git a/test/expPipelineMapTest.js b/test/expPipelineMapTest.js index 97a2db6..18f0526 100644 --- a/test/expPipelineMapTest.js +++ b/test/expPipelineMapTest.js @@ -18,6 +18,14 @@ describe("mapperHelper", () => { expect(Mapper.transform(sourceObject).select([])).to.be.an.instanceof(Mapper); }); + it("sorts the properties alphabetically", () => { + return Mapper.transform(sourceObject) + .select("one", "two", "three") + .then((response) => { + expect(Object.keys(response)).to.eql(["one", "three", "two"]); + }) + }); + it("Selects properties", () => { const result = {one: 1, three: 3}; return Mapper.transform(sourceObject) @@ -84,10 +92,21 @@ describe("mapperHelper", () => { const result = { one: 1, two: 2 }; return Mapper.transform(sourceObject) .add("two", new Promise((r) => { - setTimeout(() => r(2), 4); + setTimeout(() => r(2), 0); })) .then((object) => expect(object).to.eql(result)); }); + + it("handles creation of property by function that returns a promise", () => { + const result = { one: 1, two: 2 }; + return Mapper.transform(sourceObject) + .add("two", () => { + return new Promise((r) => { + setTimeout(() => r(2), 0); + }); + }) + .then((object) => expect(object).to.eql(result)); + }); }); context("#transform", () => {