# musk-clone

> [中文简体](./README-zh.md)

Latest version **0.0.10-alpha.3** (published 2024-11-01) · 0 weekly downloads

## Install

```sh
npm install musk-clone
pnpm add musk-clone
yarn add musk-clone
bun add musk-clone
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.10-alpha.3 |
| Published | 2024-11-01 |
| First published | 2022-07-20 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 23.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Maintainers | frankkai |

## Links

- npm: https://www.npmjs.com/package/musk-clone
- Repository: https://github.com/FrankKai/musk-clone
- Homepage: https://github.com/FrankKai/musk-clone#readme
- Issues: https://github.com/FrankKai/musk-clone/issues
- npm.io page: https://npm.io/package/musk-clone

## Dependencies (3)

- [nanoid](https://npm.io/package/nanoid.md) ^3.0.0
- [random-pro.js](https://npm.io/package/random-pro.js.md) ^0.0.2
- [lodash.clonedeep](https://npm.io/package/lodash.clonedeep.md) ^4.5.0

## Recent versions

- 0.0.10-alpha.3 (latest) — 2024-11-01
- 0.0.10-alpha.4 (alpha) — 2024-11-01
- 0.0.10 — 2024-11-01
- 0.0.10-alpha.2 — 2024-11-01
- 0.0.10-alpha.0 — 2024-11-01
- 0.0.8-alpha.1 — 2024-11-01
- 0.0.9 — 2024-01-18
- 0.0.8 — 2024-01-18
- 0.0.8-alpha.0 — 2024-01-18
- 0.0.7 — 2024-01-18
- 0.0.6 — 2023-04-28
- 0.0.5 — 2023-04-28
- 0.0.4 — 2022-12-21
- 0.0.3 — 2022-12-19
- 0.0.2 — 2022-07-20
- … 4 more at https://npm.io/package/musk-clone/versions

## README

[中文简体](./README-zh.md)

# musk-clone
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

## Why musk-clone?
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!

## Online demo
https://codesandbox.io/s/musk-clone-x137mv

## Feature
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

## Install
```
npm install musk-clone --save-dev
```
```js
yarn add -D musk-clone
```

## Usage

### Basic Usage
```js
import { muskClone } from 'musk-clone'

const src = ["foo", 1, true]
const target = muskClone({source: src});
console.log(target); 
// ["foo-0v3DrX7hoOqIFaQeMDDaF", 71, true],
```

### Frequently-used Case
```js
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 }
// ]
```

### Clone multiple
```js
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
//     }
//   ]
// ]
```

### Ignore mock object key
```js
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,
//     }
//   ]
// ]
```

### Customize the return content of the key
```js
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
//     }
//   ]
// ]
```

## API
`muskClone({source, repeat = 1, ignores = [], fieldCallbacks = {}})`

```ts
{
  source: SourceType;
  repeat?: number;
  ignores?: Array<string>;
  fieldCallbacks?: Record<string, Function>;
}
```

## Example

### Array
```js
["foo", 1, true]
=>

["foo-0v3DrX7hoOqIFaQeMDDaF", 71, false]
```
### Object
```js
{ 
  foo: "str", 
  bar: 1, 
  val: true 
}
=>

{
  foo: "str-I5s0VjK7209eBqV6QYUeJ",
  bar: 68,
  val: true
}
```
### Object Array
```js
[
  { 
    foo: "str", 
    bar: 1, 
    val: true 
  }
]
=>
[
  {
    foo: "str-eALWWUK2tsA6sn_aSAB3I",
    bar: 38,
    val: false
  }
]
```
### Complex Nest Data Structure
```js
[
  { 
    foo: "str", 
    bar: 1, 
    val: true, 
    nest: [
      { 
        baz: 2 
      }
    ] 
  }
]
=>
[
  {
    foo: "str-67nhBc5A9cDZl3dQ7L48c",
    bar: 42,
    val: true,
    nest: [
      {
        baz: 74
      }
    ]
  }
]

```

## Other
Welcome to pull request or create issue
## License

[MIT](LICENSE).

---
_Source: https://npm.io/package/musk-clone · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
