1.6.10 • Published 2 years ago

@yodata/transform v1.6.10

Weekly downloads
36
License
MIT
Repository
-
Last release
2 years ago

@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
  • key string option.name
  • value any option.value

Returns Context Context

getOption

Get an option value

Parameters
  • key string option.name
  • defaultValue any? optional defaultValue

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

1.6.10

2 years ago

1.6.9

3 years ago

1.6.8

3 years ago

1.6.7

3 years ago

1.6.6

4 years ago

1.6.2-alpha.0

4 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.6

5 years ago

1.5.5

5 years ago

1.5.4

5 years ago

1.5.3

5 years ago

1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.0.1-alpha.2

5 years ago

1.0.1-alpha.0

5 years ago

1.0.0-alpha.0

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.20

5 years ago

0.2.19

5 years ago

0.2.18

5 years ago

0.2.17

5 years ago

0.2.16

5 years ago

0.2.15

5 years ago

0.2.14

5 years ago

0.2.13

5 years ago

0.2.12

5 years ago

0.2.11

5 years ago

0.2.10

5 years ago

0.2.8

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.4-alpha.2

5 years ago

0.1.4-alpha.1

5 years ago

0.1.4-alpha.0

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago