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

json-shaper

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-shaper - npm Package Compare versions

Comparing version 1.0.3 to 1.1.0

test/fixtures/fn.js

56

dist/index.js

@@ -14,2 +14,6 @@ 'use strict';

var isFunction = exports.isFunction = function isFunction(fn) {
return typeof fn === 'function';
};
// map through all object keys and run a function

@@ -36,32 +40,36 @@ var forEach = exports.forEach = function forEach(obj, fn) {

// run extraction function if found
if (isFunction(current)) {
functionCode += '"' + propertyName + '": ' + current + '(data), ';
}
// if it's an object
if (isObject(current)) {
else if (isObject(current)) {
// it's not the root object
if (propertyName) {
// so write the property name
functionCode += '"' + propertyName + '": ';
}
// it's not the root object
if (propertyName) {
// so write the property name
functionCode += '"' + propertyName + '": ';
}
// open object
functionCode += '{ ';
// open object
functionCode += '{ ';
// go through all properties and run recursion
forEach(current, function (nextProperty, nextPropertyName) {
recursiveWalk(nextProperty, nextPropertyName);
});
// go through all properties and run recursion
forEach(current, function (nextProperty, nextPropertyName) {
recursiveWalk(nextProperty, nextPropertyName);
});
// close object
functionCode += ' }';
// close object
functionCode += ' }';
// only add comma if it's not the root object
if (propertyName) {
functionCode += ', ';
// only add comma if it's not the root object
if (propertyName) {
functionCode += ', ';
}
}
}
// otherwise it's a property only
else {
// assign value
functionCode += '"' + propertyName + '": data[ "' + current + '" ], ';
}
// otherwise it's a property only
else {
// assign value
functionCode += '"' + propertyName + '": data[ "' + current + '" ], ';
}
}

@@ -80,3 +88,3 @@

// generate executable function
return new Function('data', 'return ' + fnString);
return new Function('data', 'require', 'return ' + fnString);
};

@@ -83,0 +91,0 @@

@@ -9,3 +9,7 @@

export const isFunction = (fn) => {
return typeof fn === 'function'
}
// map through all object keys and run a function

@@ -36,4 +40,8 @@ export const forEach = (obj, fn) => {

// run extraction function if found
if (isFunction(current)) {
functionCode += '"' + propertyName + '": ' + current + '(data), '
}
// if it's an object
if (isObject(current)) {
else if (isObject(current)) {

@@ -82,3 +90,3 @@ // it's not the root object

// generate executable function
return new Function('data', `return ${fnString}`)
return new Function('data', 'require', `return ${fnString}`)
}

@@ -85,0 +93,0 @@

@@ -103,2 +103,10 @@

lastName: 'last_name',
fullName: (data) => {
const { toUpper } = require( '../test/fixtures/fn')
if (data.first_name && data.last_name) {
return toUpper(`${data.first_name} ${data.last_name}`)
}
return '-incomplete-'
},
workplace: {

@@ -126,8 +134,11 @@ id: 'office_id',

const fnCode = shaperFunction(schema)
const fnCode2 = shaperFunction(schema2)
const fnCode3 = shaperFunction(schema3)
const fnCode = shaperFunction(schema, global.require).replace(/\n/g, ' ');
const fnCode2 = shaperFunction(schema2, global.require).replace(/\n/g, ' ');
const fnCode3 = shaperFunction(schema3, global.require).replace(/\n/g, ' ');
expect(fnCode).to.equal(
'{ "id": data[ "id" ], "firstName": data[ "first_name" ], "lastName": data[ "last_name" ], ' +
'"fullName": function fullName(data) { var _require = require(\'../test/fixtures/fn\'), ' +
'toUpper = _require.toUpper; if (data.first_name && data.last_name) { return ' +
'toUpper(data.first_name + \' \' + data.last_name); } return \'-incomplete-\'; }(data), ' +
'"workplace": { "id": data[ "office_id" ], "name": data[ "office_company_name" ], "address": ' +

@@ -152,3 +163,3 @@ '{ "street": data[ "company_street" ], "postCode": data[ "company_postcode" ], "country": ' +

const transformer = shaper(schema)
const result = fixtureInput.map(transformer)
const result = fixtureInput.map(input => transformer(input, require))

@@ -155,0 +166,0 @@ expect(result).to.deep.equal(fixtureOutput)

{
"name": "json-shaper",
"version": "1.0.3",
"version": "1.1.0",
"description": "Shape a flat array into a molded json shape with deep nesting",

@@ -5,0 +5,0 @@ "author": "Chris Kalmar <christian.kalmar@gmail.com>",

@@ -53,2 +53,3 @@

lastName: 'Dow',
fullName: 'JOHN DOW',
workplace: {

@@ -69,2 +70,3 @@ id: 12,

lastName: '',
fullName: '-incomplete-',
workplace: {

@@ -85,2 +87,3 @@ id: 532,

lastName: 'Dot',
fullName: 'JANE DOT',
workplace: {

@@ -101,2 +104,3 @@ id: 152,

lastName: undefined,
fullName: '-incomplete-',
workplace: {

@@ -117,2 +121,3 @@ id: undefined,

lastName: undefined,
fullName: '-incomplete-',
workplace: {

@@ -133,2 +138,3 @@ id: undefined,

lastName: undefined,
fullName: '-incomplete-',
workplace: {

@@ -135,0 +141,0 @@ id: undefined,

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