Skip to content
This repository has been archived by the owner on Jul 5, 2022. It is now read-only.

aravindanve/squiggly-transform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Squiggly Transform

Coverage Status

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 to undefined or null.

  • when undefinedToNull is true, falls back to null for all objects with no keys or with all keys set to undefined or null.

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

About

Schema based object transforms in JS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published