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

unmarshaller

Package Overview
Dependencies
Maintainers
4
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unmarshaller - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

2

lib/index.js

@@ -1,1 +0,1 @@

'use strict';var _typeof='function'==typeof Symbol&&'symbol'==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&'function'==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?'symbol':typeof obj},_extends=Object.assign||function(target){for(var source,i=1;i<arguments.length;i++)for(var key in source=arguments[i],source)Object.prototype.hasOwnProperty.call(source,key)&&(target[key]=source[key]);return target},builder={string:function string(name,options){return _extends({name:name,type:'string'},options)},boolean:function boolean(name,options){return _extends({name:name,type:'boolean'},options)},number:function number(name,options){return _extends({name:name,type:'number'},options)},object:function object(name,options){return _extends({name:name,type:'object'},options)},holder:function holder(){var children=0<arguments.length&&arguments[0]!==void 0?arguments[0]:{};return{children:children,type:'holder'}}};exports.__esModule=!0;function unmarshal(lookupFn,unmarshaller){if('function'!=typeof lookupFn)throw new Error('lookupFn must be a function, '+('undefined'==typeof lookupFn?'undefined':_typeof(lookupFn))+' given');if('object'!==('undefined'==typeof unmarshaller?'undefined':_typeof(unmarshaller)))throw new Error('unmarshaller must be an object, '+('undefined'==typeof unmarshaller?'undefined':_typeof(unmarshaller))+' given');var keys=Object.keys(unmarshaller),res=keys.reduce(function(acc,key){var _unmarshaller$key=unmarshaller[key],parser=_unmarshaller$key.parser,name=_unmarshaller$key.name,type=_unmarshaller$key.type,defaultValue=_unmarshaller$key.defaultValue,children=_unmarshaller$key.children,of=_unmarshaller$key.of,value=lookupFn(name);if('holder'===type)return acc[key]=unmarshal(lookupFn,children),acc;if(void 0===value||null===value)void 0!==defaultValue&&null!==defaultValue&&(acc[key]=defaultValue);else if(parser){if('function'!=typeof parser)throw new Error('parser for type '+type+' must be a function, '+('undefined'==typeof parser?'undefined':_typeof(parser))+' given');acc[key]=parser(value)}else acc[key]=castIntoType(type,value);return of&&-1===of.indexOf(acc[key])&&(acc[key]=defaultValue||null),acc},{});return Object.freeze(res)}function castIntoType(type,value){switch(type){case'string':return value+'';case'number':return parseFloat(value);case'boolean':return'true'==value||!0==value;case'object':try{return'object'===('undefined'==typeof value?'undefined':_typeof(value))?value:JSON.parse(value)}catch(e){return console.error(new Error(value+' cannot be cast into '+type+': '+e)),{}}default:throw new Error(value+' cannot be cast into '+type);}}function extend(initialHolder){var additionalChildren=1<arguments.length&&arguments[1]!==void 0?arguments[1]:{};return builder.holder(_extends({},initialHolder.children,additionalChildren))}exports.builder=builder,exports.unmarshal=unmarshal,exports.castIntoType=castIntoType,exports.extend=extend;
'use strict';var _typeof='function'==typeof Symbol&&'symbol'==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&'function'==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?'symbol':typeof obj},_extends=Object.assign||function(target){for(var source,i=1;i<arguments.length;i++)for(var key in source=arguments[i],source)Object.prototype.hasOwnProperty.call(source,key)&&(target[key]=source[key]);return target},builder={string:function string(name,options){return _extends({name:name,type:'string'},options)},boolean:function boolean(name,options){return _extends({name:name,type:'boolean'},options)},number:function number(name,options){return _extends({name:name,type:'number'},options)},object:function object(name,options){return _extends({name:name,type:'object'},options)},holder:function holder(){var children=0<arguments.length&&arguments[0]!==void 0?arguments[0]:{};return{children:children,type:'holder'}},or:function or(){for(var _len=arguments.length,children=Array(_len),_key=0;_key<_len;_key++)children[_key]=arguments[_key];return{children:children,type:'or'}}};exports.__esModule=!0;function unmarshal(lookupFn,unmarshaller){if('function'!=typeof lookupFn)throw new Error('lookupFn must be a function, '+('undefined'==typeof lookupFn?'undefined':_typeof(lookupFn))+' given');if('object'!==('undefined'==typeof unmarshaller?'undefined':_typeof(unmarshaller)))throw new Error('unmarshaller must be an object, '+('undefined'==typeof unmarshaller?'undefined':_typeof(unmarshaller))+' given');var keys=Object.keys(unmarshaller),res=keys.reduce(function(acc,key){var _unmarshaller$key=unmarshaller[key],parser=_unmarshaller$key.parser,name=_unmarshaller$key.name,type=_unmarshaller$key.type,defaultValue=_unmarshaller$key.defaultValue,children=_unmarshaller$key.children,of=_unmarshaller$key.of,value=void 0;if(value='or'===type?children.reduce(function(acc,child){if(acc)return acc;var value=lookupFn(child.name);return null!=value&&''!==value?value:acc},null):lookupFn(name),'holder'===type)return acc[key]=unmarshal(lookupFn,children),acc;if(void 0===value||null===value)void 0!==defaultValue&&null!==defaultValue&&(acc[key]=defaultValue);else if(parser){if('function'!=typeof parser)throw new Error('parser for type '+type+' must be a function, '+('undefined'==typeof parser?'undefined':_typeof(parser))+' given');acc[key]=parser(value)}else acc[key]=castIntoType(type,value);return of&&-1===of.indexOf(acc[key])&&(acc[key]=defaultValue||null),acc},{});return Object.freeze(res)}function castIntoType(type,value){switch(type){case'string':return value+'';case'number':return parseFloat(value);case'boolean':return'true'==value||!0==value;case'object':try{return'object'===('undefined'==typeof value?'undefined':_typeof(value))?value:JSON.parse(value)}catch(e){return console.error(new Error(value+' cannot be cast into '+type+': '+e)),{}}case'or':return value;default:throw new Error(value+' cannot be cast into '+type);}}function extend(initialHolder){var additionalChildren=1<arguments.length&&arguments[1]!==void 0?arguments[1]:{};return builder.holder(_extends({},initialHolder.children,additionalChildren))}exports.builder=builder,exports.unmarshal=unmarshal,exports.castIntoType=castIntoType,exports.extend=extend;
{
"name": "unmarshaller",
"version": "1.1.0",
"version": "1.2.0",
"keywords": [

@@ -5,0 +5,0 @@ "config",

@@ -66,2 +66,3 @@ # Unmarshaller

| holder |
| or |

@@ -76,2 +77,15 @@ #### Default options

### Or
```js
const unmarshaller = {
foo: builder.or(
builder.string('foo_a'),
builder.string('foo_b'),
)
};
```
The unmarshalling process will call the lookup function until a value (not undefined or null) is returned.
### Extending an existing holder

@@ -78,0 +92,0 @@

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