• Stars
    star
    311
  • Rank 133,747 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐ŸŒฑ Immutability in under one kilobyte

mewt

Immutability in under one kilobyte

Made with โค at @outlandish

npm version


๐ŸŒฑ Under 1kb, tiny API, zero dependencies.

๐Ÿ‘ Makes all native array methods immutable operations.

โœŒ๏ธ Two simple methods $set and $unset for objects and arrays.

๐Ÿ‘‰ Built for Node ES2015 environments. Use a bundler, transpiler, and Proxy polyfill as required.

Install

npm install --save mewt
yarn add mewt

Import

// ES2015
import immutable from 'mewt'
// CommonJS
var immutable = require('mewt')

Usage

Create an immutable instance from a JavaScript array or object.

Both objects and arrays have the $set and $unset methods.

const immutableArray = immutable([])
const immutableObject = immutable({})

immutableArray[0] = 'Van Morrison' //=> Error "array is immutable"
immutableObject.name = 'Van Morrison' //=> Error "object is immutable"

Array

Use $set and $unset to create new array with applied change.

Use all array instance methods as usual, however those that would normally return a single non-array value (pop, push, shift, unshift) will return an array containing the value and a new array (see part 2 in example below).

const arr = immutable([])

// 1. all array instance methods are available
const arr1 = arr.concat('bubble')

    console.log(arr1) //=> ['bubble']
    console.log(arr1 === arr) //=> false

// 2. methods with non-array return value (push, pop, shift, unshift)
// also return new array, accessible via destructuring
const [val, arr2] = arr1.pop()

    console.log(val) //=> 'bubble'
    console.log(arr2) //=> []
    console.log(arr2 === arr1) //=> false
    
// 3. use $set and $unset to get new array with changes
const arr3 = arr2.$set(0, 'Iggy Pop')
    
    console.log(arr3) //=> ['Iggy Pop']
    console.log(arr3 === arr2) //=> false

Object

Use $set and $unset to create new object with applied change.

const obj = immutable({})

// 1. properties are added/updated using `$set`
const obj1 = obj.$set('album', 'Hunky Dory')

    console.log(obj1) //=> {album: 'Hunky Dory'}
    console.log(obj1 === obj) //=> false

// 2. properties are deleted using `$unset`
const obj2 = obj1.$unset('album')

    console.log(obj2) //=> {}
    console.log(obj2 === obj1) //=> false

Contributors

mewt has received contributions from these wonderful people:

Balรกzs ร‰des, Scott Humphries, Michael J. Ryan, Jamie Curtis

Contributing

All pull requests and issues welcome!

If you're not sure how, check out the great video tutorials on egghead.io!

More Repositories

1

fetch-background-sync

โœŒ๏ธ Proxy fetch requests through the Background Sync API
JavaScript
127
star
2

objecthistory

๐Ÿ€ Object undo, redo & change history using Proxies
JavaScript
58
star
3

msgr

๐Ÿ“ช Nifty service worker/client message utility
JavaScript
40
star
4

react-z-index

๐ŸŒ Easily manage global component z-index
JavaScript
24
star
5

carbonate

๐ŸŽจ colourful sprintf
JavaScript
22
star
6

werd

๐Ÿ“™ Words API for JavaScript w/ CLI (www.wordsapi.com)
JavaScript
21
star
7

tiny-tim

โณ a tiny timer < 160 bytes
JavaScript
21
star
8

cache-control

create cache-control headers
JavaScript
18
star
9

wait-for-decorator

โฐ Decorate a class method to return and wait on a Promise on the class instance before executing
JavaScript
16
star
10

daily-poem

๐Ÿ“– Read a new poem every day! (from http://poems.com)
JavaScript
12
star
11

react-floating-suffix

โ˜๏ธ Don't let that suffix outta sight!
JavaScript
12
star
12

sw-register

๐ŸŽฏ Service Worker registration utility
JavaScript
11
star
13

elquire

๐Ÿ Require local Node dependencies by a given name instead of a relative path
JavaScript
10
star
14

catch-and-match

๐Ÿ‰ Assert an error thrown (a)synchronously by a function.
JavaScript
6
star
15

require-global-node-module

Require a globally installed npm module
JavaScript
3
star
16

oss-licenses

๐Ÿ“„ A collection of OSS license texts
JavaScript
3
star
17

path-__dirname

Get a path in the __dirname
JavaScript
2
star
18

create-license

Create a LICENSE file
JavaScript
2
star
19

node-crossterm

Node bindings for Rust's crossterm
Rust
2
star
20

user-conf

Manage configs in the user home dir
JavaScript
2
star
21

path-homedir

Get a path in the user home directory
JavaScript
2
star
22

has-shebang

check if string or file contains shebang
JavaScript
2
star
23

path-cwd

Get a path in the cwd
JavaScript
2
star
24

error-clean-stack

throw errors with clean stacks
JavaScript
2
star
25

mini-defer

๐ŸŽ‰ Tiny module for creating a deferred with no polyfilling
JavaScript
1
star
26

sesd

๐Ÿ”๐Ÿ“œ Search esdiscuss.org topics
JavaScript
1
star
27

chain-call

๐Ÿ”— Build a chain of method calls
JavaScript
1
star
28

brolly

โ˜” Benchmark in style
JavaScript
1
star
29

posthog-nextjs-repro

CSS
1
star
30

i18n-squish

๐Ÿ“ฆ Watch and compile language files into single locales ready to be served with or without angular-translate
JavaScript
1
star
31

lodash-invariants

๐ŸŠ Invariants for lodash.is* methods
JavaScript
1
star
32

expand-top-level-dot-paths

Expand top-level object properties that are dot-paths
JavaScript
1
star
33

go-officialcharts

Unofficial go lib for scraping the UK singles charts from officialcharts.com
Go
1
star
34

test

JavaScript
1
star
35

markbody

๐Ÿ“ƒ Parse a webpage as markdown
JavaScript
1
star
36

webpack-get-dlls

Get DLL configs from a webpack config
JavaScript
1
star
37

argsy

๐Ÿ Awesome argument assertion
JavaScript
1
star
38

react-ref

๐Ÿ‘‰ Set reference to an element in React components
JavaScript
1
star
39

furck

๐Ÿด A better child_process.fork
JavaScript
1
star
40

require-local-node-module

Require a module from cwd's node_modules folder
JavaScript
1
star
41

serialise-response

๐Ÿ“ Serialise and deserialise a Fetch Response
JavaScript
1
star
42

series-logger

Log a series w/ spinner and op count
JavaScript
1
star
43

pick-to-array

๐Ÿœ Pick all values of a property in one or more objects
JavaScript
1
star
44

serialise-request

๐Ÿ“ Serialise and deserialise a Fetch Request
JavaScript
1
star
45

carbonate-logger

๐ŸŽจ๐Ÿ’ป Colourful console logs using carbonate
JavaScript
1
star