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.
- Host: GitHub
- URL: https://github.com/imcuttle/symbolic-link
- Owner: imcuttle
- License: mit
- Created: 2019-05-09T15:14:49.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2022-01-07T14:21:23.000Z (over 4 years ago)
- Last Synced: 2025-03-23T02:34:05.918Z (over 1 year ago)
- Language: JavaScript
- Size: 116 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: License
Awesome Lists containing this project
README
# symbolic-link
[](https://travis-ci.org/imcuttle/symbolic-link)
[](https://codecov.io/github/imcuttle/symbolic-link?branch=master)
[](https://www.npmjs.com/package/symbolic-link)
[](https://www.npmjs.com/package/symbolic-link)
[](https://prettier.io/)
[](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) 🐟