Generate a mock shadow clone with same data structure, as speed as Elon Musk.
Tesla CEO Elon Musk has 9 children, says he's doing his part to increase U.S. fertility
When you want to make a new mock item with same data structure as an exist one, you must copy --> modify one by one --> paste , this progress costs a lot time. Now you can musk-clone to save your time!
https://codesandbox.io/s/musk-clone-x137mv
According to the incoming data, quickly generate a data of the same data structure, which is used to quickly construct mock data in the development stage
- source mainly supports the two most common types, object types and array types, and supports deep nesting
- Converts only the basic types string, number, boolean, the rest return the original value with the new memory address
- Support repeat, return multiple musk-clone objects in batches
- Supports ignoring some keys of mock objects
- Supports customizing the return content of the key
npm install musk-clone --save-dev
yarn add -D musk-clone
import { muskClone } from 'musk-clone'
const src = ["foo", 1, true]
const target = muskClone({source: src});
console.log(target);
// ["foo-0v3DrX7hoOqIFaQeMDDaF", 71, true],
import { muskClone } from 'musk-clone'
const src = [
{ foo: "str", bar: 1, val: true },
{ foo: "str1", bar: 2, val: false },
]
const target = muskClone({source: src});
console.log(target);
// [
// { foo: "str-jHGKjWz3kz0ome5-tl6MS", bar: 36, val: false },
// { foo: "str1-LqOPbB5xXYKXV8hmBB_Q6", bar: 2, val: true }
// ]
import { muskClone } from 'musk-clone'
const src = [
{ foo: "str", bar: 1, val: true },
{ foo: "str1", bar: 2, val: false },
]
const target = muskClone({source: src, repeat:2});
console.log(target);
// [
// [
// {
// "foo": "str-GEoG0NphDqhwQZ44aH2LF",
// "bar": 5,
// "val": true
// },
// {
// "foo": "str1-mxD-IMD6U9BocOp8ql_XY",
// "bar": 7,
// "val": true
// }
// ],
// [
// {
// "foo": "str-ldPI7kVtHeUOMph1U4yi8",
// "bar": 3,
// "val": true
// },
// {
// "foo": "str1-EbVeOGe1YRjudpKDjA-gR",
// "bar": 7,
// "val": false
// }
// ]
// ]
import { muskClone } from 'musk-clone'
const src = [
{ foo: "str", bar: 1 },
{ foo: "str1", bar: 2 },
]
const target = muskClone({source: src, repeat: 2, ignores:['foo']});
console.log(target);
// [
// [
// {
// "foo": "str",
// "bar": 5,
// },
// {
// "foo": "str1",
// "bar": 7,
// }
// ],
// [
// {
// "foo": "str",
// "bar": 3,
// },
// {
// "foo": "str1",
// "bar": 7,
// }
// ]
// ]
const src = [
{ foo: "str", bar: 1, val: true },
{ foo: "str1", bar: 2, val: false },
];
const target = muskClone({source:src, repeat:2, ignores:["foo"], fieldCallbacks: {
bar: (item) => item + Math.random(),
}});
// [
// [
// {
// "foo": "str",
// "bar": 1.6969435733626381,
// "val": true
// },
// {
// "foo": "str1",
// "bar": 2.8654049353811537,
// "val": false
// }
// ],
// [
// {
// "foo": "str",
// "bar": 1.840926677103744,
// "val": true
// },
// {
// "foo": "str1",
// "bar": 2.9165549953686725,
// "val": false
// }
// ]
// ]
muskClone({source, repeat = 1, ignores = [], fieldCallbacks = {}})
{
source: SourceType;
repeat?: number;
ignores?: Array<string>;
fieldCallbacks?: Record<string, Function>;
}
["foo", 1, true]
=>
["foo-0v3DrX7hoOqIFaQeMDDaF", 71, false]
{
foo: "str",
bar: 1,
val: true
}
=>
{
foo: "str-I5s0VjK7209eBqV6QYUeJ",
bar: 68,
val: true
}
[
{
foo: "str",
bar: 1,
val: true
}
]
=>
[
{
foo: "str-eALWWUK2tsA6sn_aSAB3I",
bar: 38,
val: false
}
]
[
{
foo: "str",
bar: 1,
val: true,
nest: [
{
baz: 2
}
]
}
]
=>
[
{
foo: "str-67nhBc5A9cDZl3dQ7L48c",
bar: 42,
val: true,
nest: [
{
baz: 74
}
]
}
]
Welcome to pull request or create issue
MIT.