Utility library for object structure conversion.
npm install --save parse-jsA parser always starts with a call to parse(), next you can chain any transformer off of that as is required.
const parse = require('parse-js');
parse().bool().parse('true'); // true
parse().match('a').parse({
atest: 'test123',
btest: 'test456'
}); // { atest: 'test123' }Parse also accepts one argument which can be the key to extract from the object to parse. Passing this argument will automatically chain the select transformer.
const parse = require('parse-js');
parse('a-key').parse({
'a-key': 'a-value',
'some-key': 'some-value'
}); // 'a-value'This is equivalent to:
const parse = require('parse-js');
parse().select('a-key').parse({
'a-key': 'a-value',
'some-key': 'some-value'
}); // 'a-value'.parse(data)Whenever .parse() is called the configured chain will be executed on the first argument data.
If the option direction is set to REVERSE this method will simply return
the data argument as is without modifying it.
Example:
parse('test-key').base64().parse({ 'test-key': 'SGVsbG8gV29ybGQ=' });
// Hello World.reverse(sourceData)The .reverse() method will apply all the reverse methods of each transformer
and attempts to reassemble the original object based on the sourceData.
Example:
parse('test-key').base64().reverse('Hello World');
// {
// 'test-key': 'SGVsbG8gV29ybGQ='
// }.transform(parser, [reverser])This method allows you to chain your own custom parser and reverser. Both
the parser and reverser take one argument as input which is the value to
be parsed or reversed.
Instead of supplying both methods as separate arguments you can also pass
the parser and reverser as an object with both the keys defined.
Example:
// Increment / decrement transformer
function parser(v) { return v + 1; };
function reverser(v) { return v - 1; };
parse().transform(parser, reverser).parse(1); // 2
parse().transform(parser, reverser).reverse(3); // 2
// or alternatively
const transformer = {
parser: parser,
reverser: reverser
}
parse().transform(transformer).parse(1); // 2.chain(configurator)The .chain() method allows you to create predefined chains which can be
easily re-used for different parsers.
Example:
This example creates a predefined one-way base64 parser.
function base64_decode(p) {
return p.base64().setOption('direction', 'PARSE');
}
parse().select('some-key')
.chain(base64_decode)
.parse({ 'some-key': 'SGVsbG8gV29ybGQ=' }); // Hello WorldBoth parse-js instances and the parse method have methods to set global
options setOption and getOption, these can be used to configure
transformers that have global settings.
Parse.setOption(key, value);
Parse.getOption(key);
parse().setOption(key, value);
parse().getOption(key);Currently only the multilingual transformer has such options.
The behaviour of a parse-js chain can be altered using the direction option
which configures in which direction the transformers should be applied. When
set to PARSE the .reverse() calls will not touch the data supplied. Similarly
setting direction to REVERSE will leave the data untouched as .parse() is
called. By default both directions are enabled and the option is set to ANY.
parse().select(key)Selects a value from the object provided to the final .parse() call, the key supplied here can be any key supported by lodash get.
parse().default(defaultValue, reverseDefaultValue)Sets a default value when either the selected value or the reversed value is undefined.
defaultValuethe value to return whenever the selected value isundefinedreverseDefaultValuethe value to return whenever the value to reverse isundefined
parse().match(valueToMatch)Only selects those properties from an object that match valueToMatch.
valueToMatchcan either be a string or a regular expression.
parse().rename(nameParser, nameReverser);Converts key names using a function for each transition.
nameParser(key, value)will be called with the original key and value as arguments and should return the new key.nameReverser(key, value)will be called with the generated key and the value set on the object and should return the key to which this value should be written.
parse().map(callback)Map will go over each key of a select value and call the callback function with a new instance of parse-js specific to that key.
callback(parse)will be called with a new instance of parse for each key, which you can then customize by adding new chained transformers.
Example
parse().map(p => p.number()).parse({
a: '123',
b: 0,
c: '12.222,3'
}); // { a: 123, b: 0, c: 12222.3 }parse().group(regex, key, index)
group will go over each key of an object matching it against regular expression regex. If it matches, the value will be stored at result[match[key]][match[index]], where match is the set of matching groups from regex. If a key does not match the regex, it will be re-attached to the object untouched.
- The
keyargument should be set to the index of the matching group that selects the new key to use. - The
indexarguments should be set to the index of the matching group that selects the sub-key under which to store the value.
Example:
parse().group(/(a|b|c)-(name|value)/, 1, 2).parse({
'a-name': 'a-name',
'b-name': 'b-name',
'c-value': 'c-value',
'b-value': 'b-value',
'c-name': 'c-name',
'a-value': 'a-value'
});
// {
// a: { value: 'a-value', name: 'a-name' },
// b: { value: 'b-value', name: 'b-name' },
// c: { value: 'c-value', name: 'c-name' }
// }parse().oneOf(parsers, [options = {}])oneOf lets you define multiple parsers of which the first in the list with a
result that is valid according to the test option will be used.
parsersan array ofparse-jsparsers to go through.optionstesta method which returns true if the result of a parser is valid. (default:!isEmpty(v))reverseAllcontrols whether all parsers are called to reverse the value or only the first one. (default:true)
Example:
parse().oneOf([
parse().select('givenName').string(),
parse().select('firstName').string(),
parse().select('email')
]).parse({ 'firstName': 'John', email: '[email protected]' });
// 'John'parse().equals(valueToMatch, [options = {}])If the selected value matches valueToMatch it will return true, if it does not it will return false.
valueToMatchcan be either a regular expression, function or simply any value.optionscan be used to change the behaviour of the transformer.strictwill ensure===comparison is used when comparing. (default:false).reversecan be set to the value that should be set when reversing atruevalue. (default:valueToMatch).
Example:
parse().equals('some-value').parse('some-other-value'); // false
parse().equals('some-value').parse('some-value'); // trueparse().constant(constantValue, [options = {}])Always returns constantValue from parse and reverse.
The value returned from reverse can be different from constantValue
using the reverseValue option.
constantValuethe value that will be returned by this transformer.optionsreverseValueIfreverseshould return a different value it can be configured using this option.
Example:
parse().constant('a-constant').parse('some-value'); // 'a-constant'
parse().constant('a-constant', {
reverseValue: 'b-constant'
}).reverse('some-value'); // 'b-constant'parse().date([nowOnInvalid = false])Converts the selected value into a javascript date object.
nowOnInvalidIf set to true will return the current date-time whenever the value being parsed is not a valid date.
parse().bool([options = {}])Converts the selected value to a boolean value.
optionsdefaultValuewhen the value being parsed isundefinedwhat should be set as the default value.reverseToconfigures the datatype to which the boolean values are reversed. Valid options areBOOLEAN,STRINGorNUMBER.
parse().number([options = {}])Converts the selected value to a number.
optionsNaNValuethe value that will be set when the selected value can not be converted. (default:0)normalizera function used to normalize strings to number-like strings. The default handles removing multiple comma or dots in the string.basethe base in which the value is expressed. (default:10)decimalSeparatorthe sign to use as decimal separator (.or,), if not configured, the library does its best to auto-detect.
parse().string([options = {}])Converts the selected value to a string.
The selected value will be concatenated with an empty string which will call the toString() method of most values.
optionsdefaultValuethe value to return whenever the selected value isundefined.
parse().switch(cases, parseSelector, reverseSelector)Selects a different parser from the cases object by looking up the value at
parseSelector / reverseSelector in the cases object and executing it.
If the cases object does not define a key for the value returned by the selector,
the _default_ key will be called if defined. If neither of these exist undefined will be returned.
Both parseSelector and reverseSelector can be any key supported by lodash get
or a method which is passed the object to transform and the root object of this transform. Passing null or undefined will
disable the selector and .switch() will always return undefined.
casesan object containing the different possible values for the field atselector.parseSelectora key that specifies which key to look up in thecasesobject when performing a.parse()operation.reverseSelectora key that specifies which key to look up in thecasesobject when performing a.reverse()operation.
Example:
parse('test-key')
.switch({
'string': parse('value').string(),
'number': parse('value').number(),
'_default_': parse('value').string()
}, 'type', (v) => typeof v)
.parse({
'test-key': {
type: 'number',
value: '123'
}
});
// 123parse().array([options = {}])This transformer ensures that the selected value will be converted to an array. Whenever this fails it will return an empty array.
optionsmodethe methods that are allowed to be used to convert values to arrays. (default:ANY). Valid options areANY,JSONandSEPARATOR.separatorthe separator to be used whenmodeis set toANYorSEPARATOR. (default:,)
parse().base64([options = {}])Handles conversion from and to base64 strings.
optionsallowBinarywhen this option is set totruetheisPrintablecheck will be disabled. Because of this any valid base64 formatted string will be decoded.
parse().json([options = {}])Converts the selected value from and to a JSON string.
optionsdefaultValuewill be returned whenever no valid JSON string is selected.
parse().spec(specification)A specification is an object that has the desired properties of the target format, where the values are the parsers that generate the value to store with this property. This allows a source format to be converted to the desired format using parse-js.
specificationan object containingkey-parsercombinations.
Example:
parse().spec({
one: parse('two'),
two: parse('one').number(),
three: parse('four').array(),
nested: {
one: parse('one'),
two: parse('four').json()
}
}).parse({ one: '15.333.23', two: 'two', four: '[1,2,3,4,5]' });
// {
// one: 'two',
// two: 15333.23,
// three: [1, 2, 3, 4, 5],
// nested: {
// one: '15.333.23',
// two: [1, 2, 3, 4, 5]
// }
// }parse().multilingual(languages)Will group keys with language suffixes as defined by group().
languagesconfigures the languages that are supported. This option can also be set using thesetOption()method of the parse-js instance orParse.setOption(). When usingsetOption()this option is configured using the keymultilingual.languages.
Example:
parse().multilingual(['en', 'nl', 'fr', 'de']).parse({
keyEn: 'english text',
keyNl: 'dutch text'
});
// {
// key: { en: 'english text', nl: 'dutch text' }
// }parse().stripPrefix(prefix)Selects keys that start with prefix and removes that prefix from the target object.
prefixthe prefix that keys should contain and that will be removed.
Example:
parse().stripPrefix('test').parse({
atest: 'value-1',
test1: 'value-2',
test2: 'value-3'
});
// { 1: 'value-2', 2: 'value-3' }We really appreciate any contribution you would like to make, so don't hesitate to report issues or submit pull requests.
This project is released under a MIT license.
If you would like to know more about us, be sure to have a look at our website, or our Twitter accounts Ambassify, Sitebase, JorgenEvens.