# shallow-clone-shim

> Shallow clones an object while respecting the original property descriptors

Latest version **2.0.0** (published 2019-10-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install shallow-clone-shim
pnpm add shallow-clone-shim
yarn add shallow-clone-shim
bun add shallow-clone-shim
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2019-10-31 |
| First published | 2019-10-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Thomas Watson |
| Maintainers | watson |
| Keywords | clone, shallow, copy, getter, setter, configurable, enumerable, writable, property, properties, descritor, descriptors, shim, shimming, overwrite, override, monkey, patch, monkey-patch, monkeypatch |

## Links

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

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2019-10-31
- 1.1.0 — 2019-10-31
- 1.0.0 — 2019-10-28

## README

# shallow-clone-shim

Shallow clones an object, including non-enumerable properties, while
respecting the original data and accessor descriptors of the properties.
This means that for instances getters and setters are copied faithfully.
Optionally allows for shimming/overwriting properties by redefining or
manipulating existing property descriptors.

[![npm](https://img.shields.io/npm/v/shallow-clone-shim.svg)](https://www.npmjs.com/package/shallow-clone-shim)
[![build status](https://travis-ci.org/watson/shallow-clone-shim.svg?branch=master)](https://travis-ci.org/watson/shallow-clone-shim)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)

## Installation

```
npm install shallow-clone-shim --save
```

## Usage

```js
const assert = require('assert')
const clone = require('shallow-clone-shim')

const original = Object.defineProperties({}, {
  foo: { // non-writable
    value: 1
  },
  bar: { // non-configurable
    enumerable: true,
    get: function get () {
      return 2
    }
  }
})

assert.strictEqual(original.foo, 1)
assert.strictEqual(original.bar, 2)

const copy = clone({}, original, {
  bar (descriptor) {
    // descriptor == Object.getOwnPropertyDescriptor(original, 'bar')
    const getter = descriptor.get
    descriptor.get = function get () {
      return getter() + 1
    }
    return descriptor
  }
})

assert.strictEqual(original.foo, 1)
assert.strictEqual(original.bar, 3)
```

## API

### `object = clone(object, original[, shim])`

Shallow copies all own properties of the `original` into `object`. Both
enumerable and non-enumerable properties are copied.

The `object` is also returned.

If the optional `shim` argument is supplied, it's expected to be an
object containing functions. The names of the `shim` object propeties is
expected to match the names of properties in the `original` object. Each
shim function is called with the property descriptor for that particular
property in `original`. The function is expected to return a valid
property descriptor as expected by [`Object.defineProperty()`]. The
returned desciptor will replace the original descriptor in the copied
object.

## License

[MIT](LICENSE)

[`Object.defineProperty()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty

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