awesome-json2json
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -6,2 +6,3 @@ export declare type Template<T = any> = IFullTemplate<T> | string | Function; | ||
$disable?: Function; | ||
$default?: any; | ||
[propName: string]: Template; | ||
@@ -8,0 +9,0 @@ } |
@@ -40,2 +40,10 @@ "use strict"; | ||
} | ||
if (!fullTemplate.$formatting && fullTemplate.$default) { | ||
var $func_1 = typeof fullTemplate.$default === 'function' | ||
? fullTemplate.$default | ||
: function () { return fullTemplate.$default; }; | ||
fullTemplate.$formatting = function (val) { return (typeof val === 'undefined') | ||
? $func_1() | ||
: val; }; | ||
} | ||
if (fullTemplate.$formatting) { | ||
@@ -42,0 +50,0 @@ if (this.isArrayTemplate(fullTemplate)) { |
{ | ||
"name": "awesome-json2json", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "An awesome json to json mapper", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
# Awesome json2json | ||
[![Build Status](https://img.shields.io/travis/xcatliu/awesome-json2json.svg)](https://travis-ci.org/xcatliu/awesome-json2json) [![npm package](https://img.shields.io/npm/v/awesome-json2json.svg)](https://www.npmjs.org/package/awesome-json2json) [![npm downloads](http://img.shields.io/npm/dm/awesome-json2json.svg)](https://www.npmjs.org/package/awesome-json2json) | ||
[![Build Status](https://img.shields.io/travis/xcatliu/awesome-json2json.svg)](https://travis-ci.org/xcatliu/awesome-json2json) [![npm package](https://img.shields.io/npm/v/awesome-json2json.svg)](https://www.npmjs.org/package/awesome-json2json) [![npm downloads](http://img.shields.io/npm/dm/awesome-json2json.svg)](https://www.npmjs.org/package/awesome-json2json) | ||
@@ -17,3 +17,3 @@ An awesome json to json mapper | ||
import json2json from 'awesome-json2json'; | ||
// const json2json = require('awesome-json2json').default; | ||
// const { default: json2json } = require('awesome-json2json'); | ||
@@ -196,3 +196,3 @@ let sourceJson = { foo: { bar: { baz: 1 }}}; | ||
}); | ||
// { | ||
// { | ||
// new_foo: { | ||
@@ -204,2 +204,24 @@ // new_bar2: 1 | ||
### Nested template with $default | ||
With `$default` keyword, we can provide a default value when a field returns `undefined`. | ||
```js | ||
json2json({ foo: { bar: 1 }}, { | ||
new_foo: { | ||
$path: 'foo', | ||
new_bar: { $path: 'bar', $default: 11 }, | ||
new_baz: { $path: 'baz', $default: 9 }, | ||
new_qux: { $path: 'qux', $default: () => 88 } | ||
} | ||
}); | ||
// { | ||
// new_foo: { | ||
// new_bar: 1 | ||
// new_baz: 9, | ||
// new_qux: 88, | ||
// } | ||
// } | ||
``` | ||
### Template with $root | ||
@@ -206,0 +228,0 @@ |
@@ -13,2 +13,3 @@ interface IContext { | ||
$disable?: Function; | ||
$default?: any; | ||
[propName: string]: Template; | ||
@@ -83,2 +84,12 @@ } | ||
if (!fullTemplate.$formatting && fullTemplate.$default) { | ||
const $func = typeof fullTemplate.$default === 'function' | ||
? fullTemplate.$default | ||
: () => fullTemplate.$default; | ||
fullTemplate.$formatting = val => (typeof val === 'undefined') | ||
? $func() | ||
: val; | ||
} | ||
if (fullTemplate.$formatting) { | ||
@@ -85,0 +96,0 @@ if (this.isArrayTemplate(fullTemplate)) { |
@@ -373,2 +373,55 @@ const assert = require('assert'); | ||
describe('defaulting values', () => { | ||
it('should use the real value if it present, and the default if not', () => { | ||
assert.deepEqual( | ||
json2json(FOO_BAR_BAZ, { | ||
foobared: { | ||
$path: 'foo.bar', | ||
bazed: { $path: 'baz', $default: 1111 }, | ||
defaulted_value: { $path: 'defaulted', $default: 4 }, | ||
defaulted_func: { $path: 'anotherdefaulted', $default: () => 88 }, | ||
defaulted_obj: { | ||
$path: 'does_not_exist', | ||
$default: {}, | ||
nested_default: { $path: 'still_not_there', $default: 99 } | ||
} | ||
}, | ||
zilch: { | ||
$path: 'foo.bar.zilch', | ||
$default: 123, | ||
$formatting: () => 456, | ||
} | ||
}), | ||
{ | ||
foobared: { | ||
bazed: 1, | ||
defaulted_value: 4, | ||
defaulted_func: 88, | ||
defaulted_obj: { | ||
nested_default: 99, | ||
} | ||
}, | ||
zilch: 456 | ||
} | ||
) | ||
}); | ||
it('should be able to do the example frome the readme', () => { | ||
const result = json2json({ foo: { bar: 1 }}, { | ||
new_foo: { | ||
$path: 'foo', | ||
new_bar: { $path: 'bar', $default: 11 }, | ||
new_baz: { $path: 'baz', $default: 9 }, | ||
new_qux: { $path: 'qux', $default: () => 88 } | ||
} | ||
}); | ||
assert.deepEqual( | ||
result, | ||
{ | ||
new_foo: { new_bar: 1, new_baz: 9, new_qux: 88 } | ||
} | ||
) | ||
}); | ||
}); | ||
describe('context in $formatting', () => { | ||
@@ -375,0 +428,0 @@ it('should have $item context in $formatting argument', () => { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59702
1220
423