A mini Typescript/Javascript library that helps you transform objects faster & easier.
Using npm
npm install @necrobits/shapeshifter
or using yarn
yarn add @necrobits/shapeshifter
// Create a shapeshifter using a schema
const shapeshifter = Shapeshifter.create(schema);
// Transform the data
const newData = shapeshifter.transform(data);
schema
is an object that describes the transformation. There are different ways to define it. Usually, the keys in the schema are the attribute names in the object. There is an exception for virtual
attributes, we will come back to that later on. Now let's learn some simple syntax first.
Just use true
to tell the Shapeshifter to copy the attribute.
{
my_attribute : true
}
Simply write the new name as value
{
my_attribute: "my_new_attribute"
}
Yes, you can also use a function to map the value
{
my_attribute: (value) => `This is a new ${value}`
}
Sometimes, you want to construct the value based on other fields. That's the time to utilize the 2. argument in the mapping function. This example transforms the first name field into full name.
{
first_name: (first_name, obj) => `${first_name} ${obj.last_name}`
}
The 3 variants above is a shorthand version for convenience and compactness. You can define the transformation using the full version like this:
{
my_attribute: {
to: "my_new_attribute",
mapping: (value) => `This is a new ${value}`
}
}
You can define a nested transformation rule using the magic key __nested__
.
- If
parents
is only a object, the Shapeshifter will transform it as usual. - If
parents
is an array of multiple parent objects, the Shapeshifter will automatically apply a map function on the array to transform every element in the array.
{
first_name: true,
last_name: true,
parents: {
__nested__: true,
first_name: true,
last_name: true
}
}
It's kinda irrational to transform first_name
into full_name
in the above example.
What if you want to keep first_name
, last_name
but also want to create a new field called full_name
? That's where virtual attributes come into play. With virtual attributes you can define new attributes.
Because those attributes are not created based on any old attributes, so we call them "virtual".
In order to define virtual attributes, you need to use the following format __virtual<name>__
.
For example:
{
first_name: true,
last_name: true,
__virtual_fullname__: {
to: "full_name",
mapping: (_, obj) => `${obj.first_name} ${obj.last_name}`
},
__virtual_fullname2__: {
to: "full_name_uppercase",
mapping: (_, obj) => `${obj.first_name} ${obj.last_name}`.toUpperCase()
}
}
MIT
shapeshifter.js's People
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.