Modules@robinpath/transform
transform

@robinpath/transform

0.1.1Node.jsPublic

Data transformation and mapping utilities for RobinPath

Transform

Data transformation and mapping utilities: pick, omit, rename, coerce, flatten, merge, pipeline, and more

Package: @robinpath/transform | Category: Other | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the transform module when you need to:

  • Pick specific keys from an object (supports nested paths with dot notation) -- Use transform.pick to perform this operation
  • Create a copy of an object with specific keys removed -- Use transform.omit to perform this operation
  • Rename keys in an object -- Use transform.rename to perform this operation
  • Apply transformations to specific values in an object -- Use transform.mapValues to perform this operation
  • Coerce a value to a target type -- Use transform.coerce to perform this operation

Quick Reference

FunctionDescriptionReturns
pickPick specific keys from an object (supports nested paths with dot notation)New object with only the specified keys
omitCreate a copy of an object with specific keys removedNew object without the specified keys
renameRename keys in an objectObject with renamed keys
mapValuesApply transformations to specific values in an objectObject with transformed values
coerceCoerce a value to a target typeThe coerced value
flattenFlatten a nested object into a single level with dot-notation keysFlat object with concatenated key paths
unflattenUnflatten a dot-notation object back into a nested structureNested object
mergeDeep merge multiple objects (later objects override earlier ones)Deep-merged result
defaultsFill in missing/null/undefined keys from default valuesObject with defaults applied for missing keys
templateRender a template string with {{key}} placeholders replaced by data valuesRendered string with placeholders replaced
groupGroup an array of objects by a key valueObject where keys are group values and values are arrays of matching items
pipelineApply a series of transformation steps to dataThe transformed data after all steps
mapArrayMap an array of objects to a new shape by specifying target-to-source key mappingArray of reshaped objects
filterFilter an array of objects by matching key-value conditionsFiltered array of matching objects
sortSort an array of objects by a keySorted array

Functions

pick

Pick specific keys from an object (supports nested paths with dot notation)

Module: transform | Returns: object -- New object with only the specified keys

transform.pick $data ["name", "email", "address.city"]
ParameterTypeRequiredDescription
objectobjectYesSource object
keysarrayYesArray of key names or comma-separated string

omit

Create a copy of an object with specific keys removed

Module: transform | Returns: object -- New object without the specified keys

transform.omit $data ["password", "secret"]
ParameterTypeRequiredDescription
objectobjectYesSource object
keysarrayYesArray of key names to remove

rename

Rename keys in an object

Module: transform | Returns: object -- Object with renamed keys

transform.rename $data {"firstName": "first_name", "lastName": "last_name"}
ParameterTypeRequiredDescription
objectobjectYesSource object
mappingobjectYesOld-to-new key name mapping

mapValues

Apply transformations to specific values in an object

Module: transform | Returns: object -- Object with transformed values

transform.mapValues $data {"age": "toNumber", "name": "trim"}
ParameterTypeRequiredDescription
objectobjectYesSource object
mappingobjectYesKey-to-transform mapping (toString, toNumber, toBoolean, toUpperCase, toLowerCase, trim, toArray, toJSON, fromJSON)

coerce

Coerce a value to a target type

Module: transform | Returns: any -- The coerced value

transform.coerce "42" "number"
ParameterTypeRequiredDescription
valueanyYesValue to coerce
typestringYesTarget type: string, number, boolean, integer, float, array, json, object

flatten

Flatten a nested object into a single level with dot-notation keys

Module: transform | Returns: object -- Flat object with concatenated key paths

transform.flatten $nestedData "."
ParameterTypeRequiredDescription
objectobjectYesNested object
separatorstringNoKey separator (default '.')

unflatten

Unflatten a dot-notation object back into a nested structure

Module: transform | Returns: object -- Nested object

transform.unflatten {"user.name": "Alice", "user.age": 30}
ParameterTypeRequiredDescription
objectobjectYesFlat object with dot-notation keys
separatorstringNoKey separator (default '.')

merge

Deep merge multiple objects (later objects override earlier ones)

Module: transform | Returns: object -- Deep-merged result

transform.merge $defaults $userConfig $overrides
ParameterTypeRequiredDescription
objectsanyYesTwo or more objects to merge

defaults

Fill in missing/null/undefined keys from default values

Module: transform | Returns: object -- Object with defaults applied for missing keys

transform.defaults $config {"port": 3000, "host": "localhost"}
ParameterTypeRequiredDescription
objectobjectYesSource object
defaultsobjectYesDefault values to fill in

template

Render a template string with {{key}} placeholders replaced by data values

Module: transform | Returns: string -- Rendered string with placeholders replaced

transform.template "Hello {{name}}, you have {{count}} items" $data
ParameterTypeRequiredDescription
templatestringYesTemplate with {{key}} placeholders
dataobjectYesData object for placeholder values

group

Group an array of objects by a key value

Module: transform | Returns: object -- Object where keys are group values and values are arrays of matching items

transform.group $users "role"
ParameterTypeRequiredDescription
arrayarrayYesArray of objects
keystringYesKey to group by

pipeline

Apply a series of transformation steps to data

Module: transform | Returns: any -- The transformed data after all steps

transform.pipeline $data [{"action": "pick", "params": ["name","email"]}, {"action": "rename", "params": {"name": "fullName"}}]
ParameterTypeRequiredDescription
dataanyYesInput data
stepsarrayYesArray of {action, params} step objects

mapArray

Map an array of objects to a new shape by specifying target-to-source key mapping

Module: transform | Returns: array -- Array of reshaped objects

transform.mapArray $users {"fullName": "name", "mail": "email"}
ParameterTypeRequiredDescription
arrayarrayYesArray of source objects
mappingobjectYesTarget key to source key mapping

filter

Filter an array of objects by matching key-value conditions

Module: transform | Returns: array -- Filtered array of matching objects

transform.filter $users {"role": "admin", "active": true}
ParameterTypeRequiredDescription
arrayarrayYesArray of objects
conditionsobjectYesKey-value pairs that must match

sort

Sort an array of objects by a key

Module: transform | Returns: array -- Sorted array

transform.sort $users "name" "asc"
ParameterTypeRequiredDescription
arrayarrayYesArray of objects
keystringYesKey to sort by
orderstringNo'asc' or 'desc' (default 'asc')

Error Handling

All functions throw on failure. Common errors:

ErrorCause
Unknown target type: ${targetType}Check the error message for details
Cannot coerce "..." to numberCheck the error message for details
Cannot coerce "..." to integerCheck the error message for details
Cannot coerce "..." to floatCheck the error message for details
@desc "Pick and validate result"
do
  set $result as transform.pick $data ["name", "email", "address.city"]
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Multi-step Transform workflow

Chain multiple transform operations together.

@desc "Pick, omit, and more"
do
  set $r_pick as transform.pick $data ["name", "email", "address.city"]
  set $r_omit as transform.omit $data ["password", "secret"]
  set $r_rename as transform.rename $data {"firstName": "first_name", "lastName": "last_name"}
  print "All operations complete"
enddo

2. Safe pick with validation

Check results before proceeding.

@desc "Pick and validate result"
do
  set $result as transform.pick $data ["name", "email", "address.city"]
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/transform

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size7.3 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Category

utilities