0.2.3 • Published 2 years ago
squiggly-transform v0.2.3
Squiggly Transform
Squiggly Transform allows you to transform objects or arrays of objects based on predetermined schema mappings.
Usage
Install using NPM:
npm i -S squiggly-transform
Basic Usage:
import { squiggly } from 'squiggly-transform';
const transform = squiggly({
name: true,
profile: {
age: '#/age',
about: '1/about'
}
});
const result = transform({
name: 'Rosa',
age: 28,
about: null,
other: 289235
});
/* result = {
name: 'Rosa'
profile: {
age: 28,
about: null
}
} */
Advanced Usage:
/* types */
interface Source {
name: string;
age: string;
website: string;
nested: {
description: string;
};
count: number;
category: string;
}
interface Target {
name: string;
nested: {
description: string;
age: string;
website: string;
};
count: number;
categoryId: number;
}
const transform = squiggly<Source, Target>({
/* explictly defining `squiggly<Source, Target>(...)`
enforces correct types in schema and in the result object */
/* map values */
name: true, // `true` copies value
nested: {
description: true, // `true` copies nested value
age: '#/age', // copies value at json-pointer
website: '1/website' // copies value at relative-json-pointer
},
/* transform with function */
count: count => count + 1, // transforms and copies value
static: () => 'Some Text', // sets static value
/* map and transform with [json-pointer, function] tuple */
categoryId: ['#/category', value => getCategoryId(value)]
/* ^ a tuple of json-pointer and function allows you to
copy value at the pointed location and transform it */
});
Options:
undefinedToNull
falls back to null
for all undefined
values.
const transform = squiggly({ name: true, age: true }, {
undefinedToNull: true
});
transform({}) // = { name: null, age: null }
transform({ name: 'Amy' }) // = { name: 'Amy', age: null }
noEmptyObjects
falls back to
undefined
for all objects with no keys or with all keys set toundefined
ornull
.when
undefinedToNull
istrue
, falls back tonull
for all objects with no keys or with all keys set toundefined
ornull
.
const transform = squiggly({
name: true,
profile: { age: true, zipcode: true }
}, {
noEmptyObjects: true
});
transform({}) // = { name: undefined, profile: undefined }
transform({ name: 'Amy' }) // = { name: 'Amy', profile: undefined }
transform({ profile: { age: 26 } }) // = { name: undefined, profile: { age: 26 } }
Tests
Clone Repository:
git clone https://github.com/aravindanve/squiggly-transform
Run Tests:
npm run test