Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sdgluck committed Mar 26, 2017
0 parents commit a066030
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.idea
53 changes: 53 additions & 0 deletions README.md
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
22 changes: 22 additions & 0 deletions index.js
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))
})
}
14 changes: 14 additions & 0 deletions package.json
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"
}
}
61 changes: 61 additions & 0 deletions test.js
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()
})

0 comments on commit a066030

Please sign in to comment.