@nejs/subscript-proxy
Version:
A simple reusable proxy to allow custom subscript access
89 lines (58 loc) • 1.82 kB
Markdown
A powerful and flexible JavaScript library for allowing easy and programmatic subscript access to any object.
- Allows programmatic methods for defining how an object should react when non-existing keys are
accessed.
- Allow set traps instead of only get traps
- Allow option to simply return proxy around original object rather than modifying prototype chain
In a node js project, simply perform the following npm
install command.
```bash
npm install @nejs/subscript-proxy
```
In a browser, a script tag pointed here can get you a CDN
served version of the latest build
```html
<script type="module">
const { SubscriptProxy } = await import('https://cdn.jsdelivr.net/gh/nyteshade/ne-subscript-proxy@main/dist/subscriptproxy.mjs')
// ... use SubscriptProxy here
</script>
```
Or if you want it to automatically be injected into the window,
you can use the iffy variant.
```html
<script src="'https://cdn.jsdelivr.net/gh/nyteshade/ne-subscript-proxy@main/dist/subscriptproxy.js'"></script>
```
```js
import { SubscriptProxy } from '@nejs/subscript-proxy';
// or alternatively
const { SubscriptProxy } = require('@nejs/subscript-proxy');
// or alternatively
const { SubscriptProxy } = await import('@nejs/subscript-proxy');
// Take an object
let object = { name: 'Cocacola' }
// Choose some proxied key value pairs
SubscriptProxy.applyTo(object, [
['type', 'drink'],
['hasCalories', () => true]
])
// or
SubscriptProxy.applyTo(object, {
type: 'drink',
hasCalories() { return true },
})
// Then use
object.name // 'Cocacola'
object.type // 'drink'
object.hasCalories // true
```
MIT
Contributors are welcome! Please submit a Pull Request.