-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a066030
Showing
5 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# mewt | ||
|
||
> :ant: Immutability in under a kilobyte | ||
## Install | ||
|
||
```sh | ||
npm install --save mewt | ||
``` | ||
|
||
```sh | ||
yarn add mewt | ||
``` | ||
|
||
## Import | ||
|
||
```js | ||
// ES2015 | ||
import immutable from 'mewt' | ||
``` | ||
|
||
```js | ||
// CommonJS | ||
var immutable = require('mewt') | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
const arr = immutable([]) | ||
|
||
// all array instance methods are available | ||
const [,arr1] = arr.push('bubble') | ||
console.log(arr1 === arr) //=> false | ||
|
||
// methods with return value also return new array | ||
const [val, arr2] = arr1.pop() | ||
console.log(val) //=> 'bubble' | ||
console.log(arr2 === arr1) //=> false | ||
``` | ||
|
||
## API | ||
|
||
Create an immutable instance from a JavaScript array or object: | ||
|
||
```js | ||
let immutableArray = immutable([]) | ||
let immutableObect = immutable({}) | ||
``` | ||
|
||
### Array | ||
|
||
### Object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** Create immutable instance of array or object. | ||
* @returns {Array|Object} */ | ||
module.exports = function mewt (target) { | ||
let current = target | ||
, isArr = Array.isArray(target) | ||
, multiRet = ['push', 'pop', 'shift', 'unshift'] | ||
, clone = v => isArr ? [].concat(v) : Object.assign({}, v) | ||
|
||
let override = fn => (...args) => { | ||
let res = current[fn](...args) | ||
current = clone(current) | ||
return multiRet.includes(fn) ? [res, current] : current | ||
} | ||
|
||
if (!isArr && typeof target !== 'object') | ||
throw new Error('mewt accepts array or object') | ||
|
||
return new Proxy(target, { | ||
set: (_, prop, val) => (current = clone(current), current[prop] = val, true), | ||
get: (_, prop) => current[prop] && (current.hasOwnProperty(prop) ? current[prop] : override(prop)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "mewt", | ||
"version": "1.0.0", | ||
"description": "Immutability in under a kilobyte", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test" | ||
}, | ||
"author": "Sam Gluck <sdgluck@gmail.com>", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"tape": "^4.6.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict' | ||
|
||
const test = require('tape') | ||
const mewt = require('./index') | ||
|
||
test('exports a function', (t) => { | ||
t.equal(typeof mewt, 'function') | ||
t.end() | ||
}) | ||
|
||
test('throws without object or array', (t) => { | ||
t.throws(() => mewt(), /accepts array or object/) | ||
t.end() | ||
}) | ||
|
||
test('array', (t) => { | ||
{ | ||
// returns new array | ||
const a = [] | ||
const n = mewt([]) | ||
t.notEqual(a, n) | ||
} | ||
{ | ||
// copyWithin | ||
const a = mewt([1, 2]) | ||
const n = a.copyWithin(0, 2) | ||
t.deepEqual(n, [1, 2]) | ||
t.notEqual(a, n) | ||
} | ||
{ | ||
// fill | ||
const a = mewt([,]) | ||
const n = a.fill('') | ||
t.deepEqual(n, ['']) | ||
t.notEqual(a, n) | ||
} | ||
{ | ||
// pop | ||
const a = mewt(['']) | ||
const [str, n] = a.pop() | ||
t.equal(str, '') | ||
t.deepEqual(n, []) | ||
t.notEqual(a, n) | ||
} | ||
{ | ||
// push | ||
const a = mewt([]) | ||
const [len, n] = a.push('') | ||
t.equal(1, len) | ||
t.deepEqual(n, ['']) | ||
t.notEqual(a, n) | ||
} | ||
{ | ||
// reverse | ||
const a = mewt([1, 2]) | ||
const n = a.reverse() | ||
t.deepEqual(n, [2, 1]) | ||
t.notEqual(a, n) | ||
} | ||
t.end() | ||
}) |