Arrow keys: ← previous · → next 32 of 35
@nodedk/transform
0.1.4 stable

transform#

Map source fields to a new object and normalize their values.

Install#

npm i @nodedk/transform

Usage#

var transform = require('@nodedk/transform')

var schema = [
  { from: 'organisasjonsnummer', to: 'orgnr', type: 'string' },
  { from: 'navn', to: 'name', type: 'string' }
]

var data = {
  organisasjonsnummer: '934012909',
  navn: 'Tekki AS'
}

var result = transform(schema, data)
// { orgnr: '934012909', name: 'Tekki AS' }

Examples#

Arrays#

Use items to type array elements. Primitives use items.type; arrays of objects use items.schema (and nest as deep as you need).

var schema = [
  {
    from: 'ba_street',
    to: 'address_lines',
    type: 'array',
    items: { type: 'string' }
  },
  {
    from: 'notations',
    to: 'notes',
    type: 'array',
    items: {
      type: 'object',
      schema: [
        { from: 'infotype', to: 'type', type: 'string' },
        { from: 'tekst', to: 'text', type: 'string' },
        { from: 'innfoertDato', to: 'entered_date', type: 'string' }
      ]
    }
  }
]

var data = {
  ba_street: [' Strandgata 21 ', 'Postboks 32'],
  notations: [
    {
      infotype: ' STYR ',
      tekst: ' Virksomheten har fått pålegg. ',
      innfoertDato: '2026-01-27'
    }
  ]
}

var result = transform(schema, data)
// {
//   address_lines: ['Strandgata 21', 'Postboks 32'],
//   notes: [{ type: 'STYR', text: 'Virksomheten har fått pålegg.', entered_date: '2026-01-27' }]
// }

API#

transform(schema, data)#

Returns a new object synchronously. schema is an array of rules; falsy data is treated as {}. Each rule accepts:

  • from: source path, using dots for nested properties.
  • to: output property name, or function (result, value) for custom assignment. String names are literal keys, including dots.
  • type: normalization type. Omitted or unknown types preserve the source value.
  • items: optional array conversion: { type } for scalar items, or { type: 'object', schema } for nested objects.

Missing paths supply undefined to normalization. Supported types:

  • string: trimmed string; nullish values become ''.
  • number: numeric conversion; nullish values and NaN become 0.
  • boolean: strings true, 1, and yes become true, ignoring case; other strings become false. Other values use boolean conversion.
  • date: Date object, or null for missing or invalid input.
  • email: trimmed lowercase string, default ''.
  • url: trimmed string; adds http:// when no scheme:// is present. Missing input becomes ''.
  • phone: strips non-digits, preserves a leading +, and converts leading 00 to +.
  • array: trims string items; non-arrays become [].
  • null: always null.
var transform = require('@nodedk/transform')

var schema = [
  { from: 'profile.email', to: 'email', type: 'email' },
  {
    from: 'name',
    type: 'string',
    to: function (result, value) {
      result.displayName = value
    }
  }
]

console.log(
  transform(schema, {
    profile: { email: ' ADA@EXAMPLE.COM ' },
    name: ' Ada '
  })
)
// { email: 'ada@example.com', displayName: 'Ada' }

Created by Vidar Eldøy