@yodata/transform
declarative, JSON transformation library
Last updated 4 months ago by yodaveduran .
MIT · Original npm · Tarball · package.json
$ cnpm install @yodata/transform 
SYNC missed versions from official npm registry.

@yodata/transform

declarative, composable JSON transformation utility.

Installation

> npm install --save @yodata/transform

Usage

const {Context, pluginDefaultValues} = require('@yodata/transform')

const input = {
    Name: 'Bruce Wayne',
    Address: '1007 Mountain Drive',
    City: 'Gotham City',
    State: 'NJ',
    Zip: '00001',
    Phone: '1-800-4-BATMAN'
}

const toPerson = new Context({
    Name: 'name',
    Address: 'streetAddress',
    City: 'addressLocality',
    State: 'addressRegion',
    Zip: 'postalCode',
    Phone: 'telephone'
})

toPerson.map(input)
// result
{
  name: 'Bruce Wayne',
  streetAddress: '1007 Mountain Drive',
  addressRegion: 'NJ',
  addressLocality: 'Gotham City',
  postalCode: '00001',
  telephone: '1-800-4-BATMAN'
}

Features

Table of Contents

fromString

Attempt to parse a string as yaml or json

Parameters

  • value string the value to parse
  • options object? optionsal js-yaml parserOptions (optional, default {json:true})

compileContext

Fetch a conext by path or url and return an async context.map helper function

Parameters

Returns Promise<function>

map

Parameters

  • object

pathArray

Converts a path expression a.b.c to a path array [a,b,c]

Parameters

Returns Array<string> pathArray

Context

Creates a new Context

Parameters

  • contextDefinition object? a valid ContextDefinition object (optional, default {})
  • options object? configuration options (optional, default {})

parseContext

Parse & normalize a context definition JSON string

Parameters
  • contextDefinition object a valid ContextDefinition

Returns object normalized ContextDefinition document

extend

Creates a new context from the current + the provided context definition document.

Parameters
  • cdef object a valid context definition object
  • options object? optional configuration and plugin options

Returns Context a new Context

toJSON

Returns JSON representation of the Context Definition

Returns string JSON ContextDefinition

toJS

Returns a plain javascript object representation of the current context

Returns object JavaScript object representation of the current context

setOption

Sets or creates a context.options.key value

Parameters

Returns Context Context

getOption

Get an option value

Parameters

Returns any option.value | defaultValue

has

Checks the current context for key

Parameters

Returns boolean true if the current context contains key

get

Returns the value of key from the current context

Parameters
  • key (string | Array<string>) the property to find
  • defaultValue any? optional defaultValue

Returns any key.value | defaultValue

isRemoved

True if the provided key will be removed on transform

Parameters

Returns boolean true if the property found in the current context

isRedacted

True if the provided key will be redacted on transform

Parameters

Returns boolean true if property.redact = true

allowsAdditionalProperties

True if the context allows out-of-context values

isAllowed

Parameters

Returns boolean

mapKey

Returns the context name of key

Parameters

Returns string

mapKeys

Returns a new copy of object with the keys renamed by the active context (this)

Parameters

Returns object

mapValue

Executes the current context for the key/value pair and returns the resulting value

Parameters
  • value any the value
  • key (string | Array<string>) the key
  • object any? (optional, default {})
  • context any?

map

Process object with the current context non-destructive - always returns a new object

Parameters

Returns object the resulting state of object

transformEntry

Adds value to target.key. if target.key does not exist it will be created. if target.key is a scalar, it will be converted to an array and the new value will be pushed if target.key is an array, the new value is pushed to target.key if target.key is an object and value is an object, target.key will be recursively processed in the same manor

Parameters
  • target object the object being transformed
  • value any the value to add to target.key
  • key string the key
  • object any the original (source) object

Returns object the next state of target

use

Adds a plugin to the context

Parameters

Returns Context the new context with plugin installed

dispatch

Emits event to plug-in handlers

Parameters
  • event string
  • data object? (optional, default {})
  • context any?

Returns Context the current context

fromYaml

Create a new context from a YAML string

Parameters

Returns any

mapEntry

Processes the key/value pair with the current context and returns the resulting key/value pair or void if the key is not allowed

Parameters
  • entry any

Returns any [{string},{*}]

isExpression

Check for jsonata expression syntax

Parameters

  • value any

Returns any true if the value is a jsonata expression

Events

Context events used by plugins

getContext

Fetch context from path or object

Parameters

Returns Promise

loadContext

Parameters

Returns any @instance Context

loadData

Load data from a path to a yaml, json, js, or href or, if not a path, try to parse the string as yaml/json

Parameters

Returns Promise<object>

fromFile

Loads a javascript object from a filepath

Parameters

  • target string source pathname or href

Returns object parsed json or yaml object

isURL

Checks if value is a string starting with http

Parameters

  • value string the string to test

Returns boolean true if string starts with 'http'

License

MIT © Dave Duran

Current Tags

  • 1.6.6                                ...           latest (4 months ago)

55 Versions

  • 1.6.6                                ...           4 months ago
  • 1.6.2-alpha.0                                ...           4 months ago
  • 1.6.1                                ...           8 months ago
  • 1.6.0                                ...           8 months ago
  • 1.5.6                                ...           9 months ago
  • 1.5.5                                ...           9 months ago
  • 1.5.4                                ...           9 months ago
  • 1.5.3                                ...           9 months ago
  • 1.5.2                                ...           9 months ago
  • 1.5.1                                ...           a year ago
  • 1.5.0                                ...           a year ago
  • 1.0.1-alpha.2                                ...           a year ago
  • 1.0.1-alpha.0                                ...           a year ago
  • 1.0.0-alpha.0                                ...           a year ago
  • 0.4.1                                ...           a year ago
  • 0.4.0                                ...           a year ago
  • 0.3.4                                ...           a year ago
  • 0.3.3                                ...           a year ago
  • 0.3.2                                ...           a year ago
  • 0.3.1                                ...           a year ago
  • 0.3.0                                ...           a year ago
  • 0.2.20                                ...           a year ago
  • 0.2.19                                ...           a year ago
  • 0.2.18                                ...           a year ago
  • 0.2.17                                ...           a year ago
  • 0.2.16                                ...           a year ago
  • 0.2.15                                ...           a year ago
  • 0.2.14                                ...           a year ago
  • 0.2.13                                ...           a year ago
  • 0.2.12                                ...           a year ago
  • 0.2.11                                ...           a year ago
  • 0.2.10                                ...           a year ago
  • 0.2.8                                ...           a year ago
  • 0.2.7                                ...           a year ago
  • 0.2.6                                ...           a year ago
  • 0.2.5                                ...           a year ago
  • 0.2.4                                ...           2 years ago
  • 0.2.3                                ...           2 years ago
  • 0.2.2                                ...           2 years ago
  • 0.2.1                                ...           2 years ago
  • 0.2.0                                ...           2 years ago
  • 0.1.4-alpha.2                                ...           2 years ago
  • 0.1.4-alpha.1                                ...           2 years ago
  • 0.1.4-alpha.0                                ...           2 years ago
  • 0.1.3                                ...           2 years ago
  • 0.1.2                                ...           2 years ago
  • 0.1.1                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
  • 0.0.8                                ...           2 years ago
  • 0.0.7                                ...           2 years ago
  • 0.0.6                                ...           2 years ago
  • 0.0.5                                ...           2 years ago
  • 0.0.4                                ...           2 years ago
  • 0.0.3                                ...           2 years ago
  • 0.0.2                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 1
Last Month 3
Dependencies (17)
Dev Dependencies (12)

Copyright 2014 - 2016 © taobao.org |