An open API service indexing awesome lists of open source software.

https://github.com/imcuttle/symbolic-link

Make the property links to other target like the symbolic link.
https://github.com/imcuttle/symbolic-link

Last synced: over 1 year ago
JSON representation

Make the property links to other target like the symbolic link.

Awesome Lists containing this project

README

          

# symbolic-link

[![Build status](https://img.shields.io/travis/imcuttle/symbolic-link/master.svg?style=flat-square)](https://travis-ci.org/imcuttle/symbolic-link)
[![Test coverage](https://img.shields.io/codecov/c/github/imcuttle/symbolic-link.svg?style=flat-square)](https://codecov.io/github/imcuttle/symbolic-link?branch=master)
[![NPM version](https://img.shields.io/npm/v/symbolic-link.svg?style=flat-square)](https://www.npmjs.com/package/symbolic-link)
[![NPM Downloads](https://img.shields.io/npm/dm/symbolic-link.svg?style=flat-square&maxAge=43200)](https://www.npmjs.com/package/symbolic-link)
[![Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://prettier.io/)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg?style=flat-square)](https://conventionalcommits.org)

> Make the property links to other target like the symbolic link and it works well in [mobx](https://github.com/mobxjs/mobx).

## Installation

```bash
npm install symbolic-link
# or use yarn
yarn add symbolic-link
```

## Usage

### Simple Case

```javascript
const { symbolicLink } = require('symbolic-link')

const obj = {}
const dest = { name: 'foo' }
const rm = symbolicLink(obj, 'name', dest, 'foo')

obj.name === 'foo'
obj.name = 'bar'
dest.name = 'bar'

rm()

obj.name === undefined
dest.name === 'bar'
```

### Use in mobx

```javascript
const { observable } = require('mobx')
const { defineSymbolic } = require('symbolic-link')

class Src {
@observable name
}

class Combine {
@observable obj = {
name: 'objName'
}

@observable src = defineSymbolic(new Src(), {
name: [this.obj, 'name']
})

srcX = define(new Src(), {
name: [this.obj, 'name']
})
}

const comb = new Combine()
comb.src.name === comb.obj.name
comb.srcX.name === comb.obj.name
```

## API

### symbolicLink

[index.js:86-120](https://github.com/imcuttle/symbolic-link/blob/6f46a6dccc6a06316cce5d13012fb933d53b95bd/index.js#L86-L120 "Source code on GitHub")

#### Parameters

- `srcTarget` {any}
- `srcPropName` {String|Symbol|Number}
- `destTarget` {any}
- `destPropName` {String|Symbol|Number}
- `descriptor` {object}

#### Examples

```javascript
const obj = {}
const dest = { name: 'foo' }
const rm = symbolicLink(obj, 'name', dest, 'foo')

obj.name === 'foo'
obj.name = 'bar'
dest.name = 'bar'

rm()

obj.name === undefined
dest.name === 'bar'
```

Returns **any** dispose {Function}

### symbolicTarget

[index.js:142-152](https://github.com/imcuttle/symbolic-link/blob/6f46a6dccc6a06316cce5d13012fb933d53b95bd/index.js#L142-L152 "Source code on GitHub")

#### Parameters

- `target` {any}
- `config` {{name: [destTarget, destPropName, descriptor]}}

#### Examples

```javascript
const obj = {}
const dest = { name: 'foo' }
const rm = symbolicTarget(obj, {
name: [dest, 'name']
})

obj.name === 'foo'
obj.name = 'bar'
dest.name === 'bar'
rm()

obj.name === undefined
dest.name === 'bar'
```

Returns **any** dispose {Function}

### defineSymbolic

[index.js:160-163](https://github.com/imcuttle/symbolic-link/blob/6f46a6dccc6a06316cce5d13012fb933d53b95bd/index.js#L160-L163 "Source code on GitHub")

#### Parameters

- `target` {any}
- `config` {{name: [destTarget, destPropName, descriptor]}}

Returns **any** target {any}

## Contributing

- Fork it!
- Create your new branch:
`git checkout -b feature-new` or `git checkout -b fix-which-bug`
- Start your magic work now
- Make sure npm test passes
- Commit your changes:
`git commit -am 'feat: some description (close #123)'` or `git commit -am 'fix: some description (fix #123)'`
- Push to the branch: `git push`
- Submit a pull request :)

## Authors

This library is written and maintained by imcuttle, moyuyc95@gmail.com.

## License

MIT - [imcuttle](https://github.com/imcuttle) 🐟