Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-json-transform

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-json-transform - npm Package Compare versions

Comparing version 1.0.21 to 1.1.0

33

index.js

@@ -5,3 +5,3 @@ // DataTransform

exports.DataTransform = function(data, map){
var DataTransform = function(data, map){

@@ -63,8 +63,15 @@ return {

transform : function(context) {
var value = this.getValue(data, map.list);
var useList = map.list != undefined;
var value;
if (useList) {
value = this.getValue(data, map.list);
} else if (_.isArray(data) && !useList) {
value = data;
} else if (_.isObject(data) && !useList) {
value = [data];
}
var normalized = [];
if(!_.isEmpty(value)) {
var list = this.getList();
var list = useList ? this.getList() : value;
normalized = map.item ? _.map(list, _.bind(this.iterator, this, map.item)) : list;

@@ -76,2 +83,6 @@ normalized = _.bind(this.operate, this, normalized)(context);

if(!useList && _.isObject(data) && !_.isArray(data)){
return normalized[0];
}
return normalized;

@@ -164,1 +175,13 @@

};
exports.DataTransform = DataTransform;
exports.transform = function(data, map, context) {
var dataTransform = new DataTransform(data, map)
return dataTransform.transform(context);
}
exports.transformAsync = function(data, map, context) {
var dataTransform = new DataTransform(data, map)
return dataTransform.transformAsync(context);
}

6

package.json
{
"name": "node-json-transform",
"version": "1.0.21",
"version": "1.1.0",
"description": "A node module for transforming and performing operations on JSON.",

@@ -25,8 +25,8 @@ "main": "index.js",

"dependencies": {
"lodash": "^4.17.4"
"lodash": "^4.17.15"
},
"devDependencies": {
"deep-freeze": "0.0.1",
"jasmine-node": "^1.14.5"
"jasmine-node": "^3.0.0"
}
}
# node-data-transform
A node module for transforming and performing operations on JSON.
## Usage
### Basic Example
## Installation
```javascript
npm install node-json-transform --save
```
### Get Started
```javascript
var DataTransform = require("node-json-transform").DataTransform,
var transform = require("node-json-transform").transform;
// or
var { transform } = require("node-json-transform");
var result = transform({
text: "hello"
}, {
item: {
message: "text"
}
});
// result { message: "hello" }
```
### Transform API
transform (data, map, context)
#### Parameters
|Parameter|Type|Required|Description
|---|---|---|---|
|data| Object, Array|true |The JSON data that you want to transform|
|map| Object|true|How you want to tranform it|
|context| Object | false |Context to bind to for each item transformation. |
#### Returns
Object or Array based on input.
#####Object
If an object is passed in, it will transform the object and return the resulting object.
##### Array
If an array is passed in, each item will be iterated, transformed, and the entire result will be returned. If no "list" is passed map, it will used the data as is.
### Map Schema
```javascript
{
list: "", // Not required. If there is a sub-attribute in the incoming data that you want to used for tranformation rather than the data itself, you can specify that here. It must point to an array.
item: { // Required. Defines object mapping.
destination: "source" // The destination is the attribute name where source data will be mapped to in the result. The path uses lodash.get function to find a value in the incoming data.
},
remove:['attribute'], // Not required. Specifies an attribute to be removed from each item.
defaults: {} // Not required. Specifies fallback values for attributes if they are missing.
operate:[ // Not required. Runs after object mapping. Modifies the attribute specified in "on".
{
run: "", // Specifices the name of a function to run
on: "" // Specifies the attribute to be passed into the function above as a parameter
}
],
each: function(item){ // Not required. Runs after object mapping and operations. Allows access to each item for manipulation.
return item;
}
}
```
### Common Example
First we need some data.

@@ -15,24 +72,20 @@

var data = {
posts : [
{
title : "title1",
description: "description1",
blog: "This is a blog.",
date: "11/4/2013",
extra : {
link : "http://goo.cm"
},
list1:[
{
name:"mike"
}
],
list2:[
{
item: "thing"
}
],
clearMe: "text"
}
]
title : "title1",
description: "description1",
blog: "This is a blog.",
date: "11/4/2013",
extra : {
link : "http://goo.cm"
},
list1:[
{
name:"mike"
}
],
list2:[
{
item: "thing"
}
],
clearMe: "text"
};

@@ -45,31 +98,27 @@ ```

var map = {
list : 'posts',
item: {
name: "title",
info: "description",
text: "blog",
date: "date",
link: "extra.link",
item: "list1.0.name",
clearMe: "",
fieldGroup: ['title', 'extra']
},
operate: [
{
run: "Date.parse", on: "date"
},
{
run: function(val) { return val + " more info"}, on: "info"
}
],
each: function(item){
// make changes
item.iterated = true;
return item;
}
}
item: {
name: "title",
info: "description",
text: "blog",
date: "date",
link: "extra.link",
item: "list1.0.name",
clearMe: "",
fieldGroup: ["title", "extra"]
},
operate: [
{
run: "Date.parse", on: "date"
},
{
run: function(val) { return val + " more info"}, on: "info"
}
],
each: function(item){
item.iterated = true;
return item;
}
};
```
You can read this as follows:
- Get the array of objects in "posts".
- Map the name to title, info to description etc.

@@ -81,4 +130,4 @@ - Run Data.parse on the date value.

```javascript
var dataTransform = DataTransform(data, map);
var result = dataTransform.transform();
var transform = require("node-json-transform").transform;
var result = transform(data, map);
console.log(result);

@@ -88,6 +137,5 @@ ```

```javascript
var dataTransform = DataTransform(data, map);
var promise = dataTransform.transformAsync();
promise.then((function(result){
console.log(result);
var transform = require("node-json-transform").transformAsync;
transform(data, map).then((function(result){
console.log(result);
});

@@ -99,13 +147,13 @@ ```

[
{
name : "title1",
info: "description1",
text: "This is a blog.",
date: 1383544800000,
link: "http://goo.cm",
info: "mike more info",
clearMe: "",
fieldGroup: ['title1', { link : "http://goo.cm" }],
iterated: true
}
{
name : "title1",
info: "description1",
text: "This is a blog.",
date: 1383544800000,
link: "http://goo.cm",
info: "mike more info",
clearMe: "",
fieldGroup: ["title1", { link : "http://goo.cm" }],
iterated: true
}
]

@@ -117,99 +165,95 @@ ```

```
```javascript
var map = {
list: 'items',
item: {
id: 'id',
sku: 'sku',
zero: 'zero',
toReplace: 'sku',
errorReplace: 'notFound',
simpleArray: ['id', 'sku','sku'],
complexArray: [ {node: 'id'} , { otherNode:'sku' } , {toReplace:'sku'} ],
subObject: {
node1: 'id',
node2: 'sku',
subSubObject: {
node1: 'id',
node2: 'sku',
}
},
},
remove: ['unwanted'],
defaults: {
"missingData": true
},
operate: [
{
run: (val) => 'replacement',
on: 'subObject.subSubObject.node1'
},
{
run: (val) => 'replacement',
on: 'errorReplace'
},
{
run: (val) => 'replacement',
on: 'toReplace'
},
{
run: (val) => 'replacement',
on: 'simpleArray.2'
},
{
run: (val) => 'replacement',
on: 'complexArray.2.toReplace'
}
]
item: {
id: "id",
sku: "sku",
zero: "zero",
toReplace: "sku",
errorReplace: "notFound",
simpleArray: ["id", "sku","sku"],
complexArray: [ {node: "id"} , { otherNode:"sku" } , {toReplace:"sku"} ],
subObject: {
node1: "id",
node2: "sku",
subSubObject: {
node1: "id",
node2: "sku",
}
},
},
remove: ["unwanted"],
defaults: {
"missingData": true
},
operate: [
{
run: (val) => "replacement",
on: "subObject.subSubObject.node1"
},
{
run: (val) => "replacement",
on: "errorReplace"
},
{
run: (val) => "replacement",
on: "toReplace"
},
{
run: (val) => "replacement",
on: "simpleArray.2"
},
{
run: (val) => "replacement",
on: "complexArray.2.toReplace"
}
]
};
var object = {
items:[
{
id: 'books',
zero: 0,
sku:'10234-12312',
unwanted: true
}
]
};
var object = [
{
id: "books",
zero: 0,
sku:"10234-12312",
unwanted: true
}
];
var result = DataTransform(data, map).transform();
var result = transform(data, map);
```
The expected output.
```
```javascript
[
{
"id": "books",
"sku": "10234-12312",
"zero": 0,
"toReplace": "replacement",
"errorReplace": "replacement",
"simpleArray": [
"books",
"10234-12312",
"replacement"
],
"complexArray": [
{
"node": "books"
},
{
"otherNode": "10234-12312"
},
{
"toReplace": "replacement"
}
],
"subObject": {
"node1": "books",
"node2": "10234-12312",
"subSubObject": {
"node1": "replacement",
"node2": "10234-12312"
}
},
"missingData": true
}
{
id: "books",
sku: "10234-12312",
zero: 0,
toReplace: "replacement",
errorReplace: "replacement",
simpleArray: [
"books",
"10234-12312",
"replacement"
],
complexArray: [
{
node: "books"
},
{
otherNode: "10234-12312"
},
{
toReplace: "replacement"
}
],
subObject: {
node1: "books",
node2: "10234-12312",
subSubObject: {
node1: "replacement",
node2: "10234-12312"
}
},
missingData: true
]

@@ -220,49 +264,47 @@ ```

```
var data = {
products: [{
id: 'books0',
zero: 0,
sku: '00234-12312',
subitems: [
{ subid: "0.0", subsku: "subskuvalue0.0" },
{ subid: "0.1", subsku: "subskuvalue0.1" }
]
}, {
id: 'books1',
zero: 1,
sku: '10234-12312',
subitems: [
{ subid: "1.0", subsku: "subskuvalue1.0" },
{ subid: "1.1", subsku: "subskuvalue1.1" }
]
}]
};
```javascript
var data = [
{
id: "books0",
zero: 0,
sku: "00234-12312",
subitems: [
{ subid: "0.0", subsku: "subskuvalue0.0" },
{ subid: "0.1", subsku: "subskuvalue0.1" }
]
}, {
id: "books1",
zero: 1,
sku: "10234-12312",
subitems: [
{ subid: "1.0", subsku: "subskuvalue1.0" },
{ subid: "1.1", subsku: "subskuvalue1.1" }
]
}
];
var baseMap = {
'list': 'products',
'item' : {
'myid': 'id',
'mysku': 'sku',
'mysubitems': 'subitems'
},
operate: [
{
'run': function(ary) {
return DataTransform({list:ary}, nestedMap).transform();
},
'on': 'mysubitems'
}
]
item : {
"myid": "id",
"mysku": "sku",
"mysubitems": "subitems"
},
operate: [
{
run: function(ary) {
return transform(ary, nestedMap);
},
on: "mysubitems"
}
]
};
var nestedMap = {
'list': 'list',
'item' : {
'mysubid': 'subid',
'mysubsku': 'subsku'
}
"item" : {
"mysubid": "subid",
"mysubsku": "subsku"
}
};
var result = DataTransform(data, baseMap).transform();
var result = transform(data, baseMap);
```

@@ -272,20 +314,20 @@

```
```javascript
[
{
"myid": "books0",
"mysku": "00234-12312",
"mysubitems": [
{ "mysubid": "0.0", "mysubsku": "subskuvalue0.0" },
{ "mysubid": "0.1", "mysubsku": "subskuvalue0.1"}
]
},
{
"myid": "books1",
"mysku": "10234-12312",
"mysubitems": [
{ "mysubid": "1.0", "mysubsku": "subskuvalue1.0" },
{ "mysubid": "1.1", "mysubsku": "subskuvalue1.1" }
]
}
{
"myid": "books0",
"mysku": "00234-12312",
"mysubitems": [
{ "mysubid": "0.0", "mysubsku": "subskuvalue0.0" },
{ "mysubid": "0.1", "mysubsku": "subskuvalue0.1"}
]
},
{
"myid": "books1",
"mysku": "10234-12312",
"mysubitems": [
{ "mysubid": "1.0", "mysubsku": "subskuvalue1.0" },
{ "mysubid": "1.1", "mysubsku": "subskuvalue1.1" }
]
}
]

@@ -296,17 +338,11 @@ ```

```javascript
var DataTransform = require("node-json-transform").DataTransform;
```
First we need some data.
```javascript
var data = {
posts : [
{
title : "title1",
description: "description1"
}
]
};
var data = [
{
title : "title1",
description: "description1"
}
];
```

@@ -317,20 +353,18 @@

```javascript
var map = {
list : 'posts',
item: {
name: "title",
info: "description"
},
operate: [
{
run: function(val, context) { return val + " more info for" + context.type},
on: "info"
}
],
each: function(item, index, collection, context){
// make changes
item.type = context.type;
return item;
}
};
var map = {
item: {
name: "title",
info: "description"
},
operate: [
{
run: function(val, context) { return val + " more info for" + context.type},
on: "info"
}
],
each: function(item, index, collection, context){
item.type = context.type;
return item;
}
};
```

@@ -340,6 +374,5 @@

```javascript
var dataTransform = DataTransform(data, map);
var context = { type: 'my-type' };
var result = dataTransform.transform(context);
console.log(result);
var context = { type: "my-type" };
var result = transform(data, map, context);
console.log(result);
```

@@ -349,15 +382,15 @@

```javascript
[
{
name : "title1",
info: "description1 more info for my-type",
type: 'my-type'
}
]
[
{
name : "title1",
info: "description1 more info for my-type",
type: "my-type"
}
]
```
Enjoy!
## Changelog
1.1.0 New functional transform() interface. Object support. Updated documentation.
1.0.21 Uses loadash _get() for source traversal

@@ -379,3 +412,3 @@ 1.0.20 Correct documentation

1.0.6 Accepted pull request form ooskapenaar. You can now use custom functions as operators.
1.0.5 Accepted pull request from jaymedavis. You can now pass an array directly and leave 'list' undefined.
1.0.5 Accepted pull request from jaymedavis. You can now pass an array directly and leave "list" undefined.
1.0.4 Added the ability to group fields into arrays

@@ -382,0 +415,0 @@ 1.0.3 Added the ability to clear and set field by passing an empty string in the map.

@@ -1,6 +0,5 @@

var DataTransform = require('../index.js').DataTransform,
var transform = require('../index.js').transform,
_ = require("lodash");
var map = {
list: 'items',
item: {

@@ -45,11 +44,9 @@ id: 'id',

var object = {
items:[
{
id: 'books',
zero: 0,
sku:'10234-12312'
}
]
};
var object = [
{
id: 'books',
zero: 0,
sku:'10234-12312'
}
];

@@ -94,4 +91,3 @@ describe("node-json-transform", function() {

var dataTransform = new DataTransform(object, map);
var result = dataTransform.transform();
var result = transform(object, map);

@@ -98,0 +94,0 @@ expect(result).toEqual(expected);

@@ -1,26 +0,23 @@

var DataTransform = require('../index.js').DataTransform,
var transform = require('../index.js').transform,
_ = require("lodash");
var data = {
products: [{
id: 'books0',
zero: 0,
sku: '00234-12312',
subitems: [
{ subid: "0.0", subsku: "subskuvalue0.0" },
{ subid: "0.1", subsku: "subskuvalue0.1" }
]
}, {
id: 'books1',
zero: 1,
sku: '10234-12312',
subitems: [
{ subid: "1.0", subsku: "subskuvalue1.0" },
{ subid: "1.1", subsku: "subskuvalue1.1" }
]
}]
};
var data = [{
id: 'books0',
zero: 0,
sku: '00234-12312',
subitems: [
{ subid: "0.0", subsku: "subskuvalue0.0" },
{ subid: "0.1", subsku: "subskuvalue0.1" }
]
}, {
id: 'books1',
zero: 1,
sku: '10234-12312',
subitems: [
{ subid: "1.0", subsku: "subskuvalue1.0" },
{ subid: "1.1", subsku: "subskuvalue1.1" }
]
}];
var baseMap = {
'list': 'products',
'item' : {

@@ -31,14 +28,13 @@ 'myid': 'id',

},
operate: [
{
'run': function(ary) {
return DataTransform({list:ary}, nestedMap).transform();
},
'on': 'mysubitems'
}
]
operate: [
{
'run': function(ary) {
return transform(ary, nestedMap);
},
'on': 'mysubitems'
}
]
};
var nestedMap = {
'list': 'list',
'item' : {

@@ -73,7 +69,3 @@ 'mysubid': 'subid',

it("should copy arrays", function() {
var dataTransform = DataTransform(data, baseMap);
expect(dataTransform.transform()).toEqual(expected);
expect(transform(data, baseMap)).toEqual(expected);
});

@@ -80,0 +72,0 @@

@@ -1,12 +0,9 @@

var DataTransform = require('../index.js').DataTransform,
var transform = require('../index.js').transform,
_ = require("lodash");
var data = {
posts: [{
title: "title1"
}]
};
var data = [{
title: "title1"
}];
var map = {
list: 'posts',
item: {

@@ -24,4 +21,3 @@ name: "title",

var dataTransform = DataTransform(data, map);
dataTransform.transform();
transform(data, map);

@@ -36,4 +32,3 @@ expect(clone).toEqual(data);

var dataTransform = DataTransform(data, map);
dataTransform.transform();
transform(data, map);

@@ -40,0 +35,0 @@ expect(clone).toEqual(map);

var DataTransform = require('../index.js').DataTransform,
transform = require('../index.js').transform,
_ = require("lodash");

@@ -421,2 +422,57 @@

it("should transform an object literal if list is not set and data is an object", function (){
var data = {
text: 'hello'
};
var map = {
item: {
message: 'text'
}
};
var dataTransform = DataTransform(data, map);
var result = dataTransform.transform();
expect(result).toEqual({
message: 'hello'
});
});
it("should transform via transform function", function (){
var data = {
text: 'hello'
};
var map = {
item: {
message: 'text'
}
};
var result = transform(data, map);
expect(result).toEqual({
message: 'hello'
});
});
it("should try use the map directly if no item", function (){
var data = {
text: 'hello'
};
var map = {
item: { message: 'text' }
};
var result = transform(data, map);
expect(result).toEqual({
message: 'hello'
});
});
});
var DataTransform = require('../index.js').DataTransform,
transform = require('../index.js').transform,
_ = require("lodash");

@@ -64,9 +65,14 @@

it("should always return an array", function() {
it("should pass the context to each via functional api", function() {
var dataTransform = DataTransform({}, {});
expect(_.isArray(dataTransform.transform())).toEqual(true);
var context = {
intro: 'Hi '
};
expect(transform(_.clone(data), map, context)).toEqual([{
greeting: "Hi title1"
}]);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc