Add mapProperties method to jsonary wrappers
authorFrank Bessou <frank.bessou@logilab.fr>
Mon, 15 May 2017 13:22:01 +0200
changeset 204 6de8ff025817
parent 203 777d97b1e6c1
child 205 9edd745e2e16
Add mapProperties method to jsonary wrappers
src/jsonaryutils.js
test/index.js
--- a/src/jsonaryutils.js	Mon May 15 11:24:08 2017 +0200
+++ b/src/jsonaryutils.js	Mon May 15 13:22:01 2017 +0200
@@ -17,6 +17,13 @@
     return result;
 }
 
+function mapProperties(callback) {
+    const result = [];
+    this.properties((key, property) => {result.push(callback(property, key));});
+    return result;
+}
+
 Jsonary.extendData({
     mapItems: mapItems,
+    mapProperties: mapProperties,
 });
--- a/test/index.js	Mon May 15 11:24:08 2017 +0200
+++ b/test/index.js	Mon May 15 13:22:01 2017 +0200
@@ -382,6 +382,18 @@
             expect(mapped).to.deep.equal(['one0', 3, 6]);
         });
     });
+
+    describe('mapProperties', () => {
+        it('should map items ', () => {
+            const wrappedObject = Jsonary.create({a: 1, b: "two", c: {d: 3}});
+
+            const mapped = wrappedObject.mapProperties((property, key) =>
+                ({[key]: property.value()})
+            );
+
+            expect(mapped).to.deep.equal([{a: 1}, {b: "two"}, {c: {d: 3}}]);
+        });
+    });
 });
 
 describe('Utils', () => {