Package Exports
- jsxl
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jsxl) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
jsxl is a utility module providing powerful features for filtering, validating, and transforming JSON objects in Node.js applications.
To install jsxl, in your command line, run
npm install jsxlTo start using jsxl in code, write
const jsxl = require('jsxl');Filtering, validating, and transforming happens through application of jsxl-objects to JSON-object. jsxl-objects, also refereed to as filters, are themselves JSON-objects.
The following code-snippet illustrates successful validation of an Array of Objects of Numbers,
jsxl(
{
input: [ { number: 7 }, { number: 8 }, { number: 9 } ]
},
[ { number: Number } ], // filter
(err, output) => {
console.log(output);
// yields: [ { number: 7 }, { number: 8 }, { number: 9 } ]
}
);whereas the following code.snippet illustrates unsuccessful validation of similar.
jsxl(
{
input: [ { number: 'a' }, { number: 'b' }, { number: 'c' } ]
},
[ { number: Number } ], // filter
(err, output) => {
console.log(err);
// yields: 'input[0].number must be of type Number (not String)'
}
);The following code-snippet extends these by filtering out even-numbered objects and multiplying numbers by 3 in odd-numbered objects.
jsxl(
{
input: [ { number: 7 }, { number: 8 }, { number: 9 } ]
},
[{
$filter: (object, context, next) => {
next(null, !(object && typeof object.number == 'number' && object.number % 2 == 0)); // rejects even numbers
},
$type: {
number: {
$type: Number,
$transform: (number, context, next) => {
next(null, number * 3);
}
}
},
}], // filter
(err, output) => {
console.log(output);
// yields: [ { number: 21 }, { number: 27 } ]
}
);Filters may be compiled and executed combined for single use, as illustrated in above examples, or may be compiled and executed separately for repeated use.
The following code-snippet compiles and executes the first of above filters separately.
jsxl.compile(
[ { number: Number } ], // filter
(err, filter) => {
if (err) return console.log(err);
jsxl.execute(
{
input: [ { number: 7 }, { number: 8 }, { number: 9 } ]
},
filter,
(err, output) => {
console.log(output);
// yields: [ { number: 7 }, { number: 8 }, { number: 9 } ]
}
);
}
);