Add mapItems method to Jsonary data wrappers
authorFrank Bessou <frank.bessou@logilab.fr>
Wed, 05 Apr 2017 11:44:18 +0200
changeset 80 225e9a1e59d6
parent 79 1070e0148bd8
child 81 d7da436b1dbc
Add mapItems method to Jsonary data wrappers
src/jsonaryutils.js
test/index.js
--- a/src/jsonaryutils.js	Tue Apr 04 17:00:42 2017 +0200
+++ b/src/jsonaryutils.js	Wed Apr 05 11:44:18 2017 +0200
@@ -1,4 +1,5 @@
 /* global Jsonary */
+import "script-loader!jsonary/super-bundle/jsonary-super-bundle";
 import {PropTypes} from "react";
 
 export function wrapEntityData(entity, schema) {
@@ -15,3 +16,14 @@
 }
 
 export const PropTypeJsonaryWrapper = PropTypes.object;
+
+function mapItems(callback) {
+    const result = [];
+    this.items((index, item) => {result.push(callback(item, index))});
+    return result;
+}
+
+Jsonary.extendData({
+    mapItems: mapItems,
+});
+
--- a/test/index.js	Tue Apr 04 17:00:42 2017 +0200
+++ b/test/index.js	Wed Apr 05 11:44:18 2017 +0200
@@ -14,9 +14,6 @@
 import {EntityMeta} from '../src/components/Entity';
 import {wrapEntityData} from "../src/jsonaryutils";
 
-import "script-loader!jsonary/super-bundle/jsonary-super-bundle";
-
-
 const userEditionSchema = {
     "$ref": "#/definitions/CWUser",
     "definitions": {
@@ -344,6 +341,17 @@
     });
 
 });
+describe('Jsonary Extensions', () => {
+    describe('mapItems', () => {
+        it('should map items ',() => {
+            const wrappedArray = Jsonary.create(['one', 2, 4]);
+
+            const mapped = wrappedArray.mapItems((currentItem, index)=> currentItem.value()+index);
+
+            expect(mapped).to.deep.equal(['one0', 3, 6]);
+        });
+    });
+});
 
 describe('Utils', () => {
     describe('appendPath', () => {