New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@simpleview/deep-transform

Package Overview
Dependencies
Maintainers
22
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@simpleview/deep-transform

Perform complex transformations to convert one object structure into another object based on a JSON-declared schema

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
22
Created
Source

@simpleview/deep-transform

Getting Started

npm install @simpleview/deep-transform
import transform from "@simpleview/deep-transform";

Overview

deep-transform is a utility library to convert an object or array of one structure into an entirely different array or object. This is especially useful in scenarios where you want to process the object declaratively via a JSON/Object-style definition and can't write traditional imperative .map, .filter.

See the typescript signature for deepTransform for all arguments and their functionality.

Examples:

// Process an object into another object
transform({
	foo: "fooValue",
	bar: "barValue"
}, {
	obj: {
		newFoo: ".foo",
		newBar: ".bar"
	}
}) === {
	newFoo: "fooValue",
	newBar: "barValue"
}

// Process an object reaching into deeply nested data and arrays
transform({
	foo: {
		bar: {
			baz: 10,
			items: [
				{ title: "First Title" },
				{ title: "Second Title" }
			]
		}
	}
}, {
	obj: {
		firstTitle: ".foo.baz.items[0].title",
		secondTitle: ".foo.baz.items[1].title"
	}
}) === {
	firstTitle: "First Title",
	secondTitle: "Second Title"
}

// Cast data
transform({
	foo: 10,
	bar: "10"
}, {
	obj: {
		foo: {
			key: ".foo"
			cast: "string"
		},
		bar: {
			key: ".bar",
			cast: "number"
		}
	}
}) === {
	foo: "10",
	bar: 10
}

// Process an array of objects into a simple array
transform({
	values: [
		{ label: "First", value: "1" },
		{ label: "Second", value: "2" },
		{ label: "Third", value: "3" }
	]
}, {
	key: ".values",
	each: {
		key: ".value",
		cast: "number"
	}
}) === [1, 2, 3]

// Process an array of objects into an array of objects
transform([
	{ region: "Downtown", region_id: 10 },
	{ region: "Central", region_id : 2 },
	{ region: "North", region_id: 3 }
], {
	each: {
		obj: {
			label: ".region",
			value: {
				key: ".region_id",
				cast: "string"
			}
		}
	}
}) === [
	{ label: "Downtown", value: "10" },
	{ label: "Central", value: "2" },
	{ label: "North", value: "3" }
]

// Process an object using conditional logic
var schema = {
	if: {
		".account_id": { exists: { value: true } }
	},
	then: {
		set: {
			"account.match.account_id.eq": ".account_id"
		}
	}
}

transform({
	account_id: 10
}, schema) === {
	account: {
		match: {
			account_id: {
				eq: 10
			}
		}
	}
}

transform({
	contact_id: 10
}, schema) === undefined

FAQs

Package last updated on 03 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc