├── .gitignore ├── README.md ├── bsconfig.json ├── package.json └── src └── immJsRe.re /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /node_modules/ 3 | /lib/ 4 | .merlin 5 | *.cm* 6 | *.js 7 | npm-debug.log 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bs-immutablejs 2 | 3 | **Note**: these are **bindings** to the [Immutable.js](https://facebook.github.io/immutable-js/) library. These are only meant to be used to interoperate with existing JavaScript code; In Reason/BS, we have more lightweight immutable (and optionally mutable) data structures by default (list, record, hashmap, set, and upcoming data structures). New Reason/BS projects projects don't need bs-immutablejs. 4 | 5 | ## Documentation 6 | 7 | The source is a [single file](https://github.com/reasonml-community/bs-immutablejs/blob/master/src/immJsRe.re) of BuckleScript `external`s. The API corresponds to Immutable.js' API. 8 | 9 | Example usage: 10 | 11 | ```reason 12 | let greeting = ImmutableJsRe.List.fromArray([|"hello", "world"|]); 13 | 14 | let extendedGreeting = ImmutableJsRe.List.push(greeting, "how are you"); 15 | 16 | let reallyExtendedGreeting = 17 | ImmutableJsRe.List.( 18 | extendedGreeting 19 | |. push("I'm fine") 20 | |. push("thank you") 21 | |. push("and you?") 22 | ); 23 | ``` 24 | 25 | Generated code: 26 | 27 | ```js 28 | var greeting = Immutable.List(/* array */[ 29 | "hello", 30 | "world" 31 | ]); 32 | var extendedGreeting = greeting.push("how are you"); 33 | extendedGreeting.push("I'm fine").push("thank you").push("and you?"); 34 | ``` 35 | 36 | ## Transitioning to Idiomatic Reason/BuckleScript Data Structures 37 | 38 | Here's a table of Immutable.js data structures and their Reason/BuckleScript equivalents. Reason/BS provide the data structures out of the box. 39 | 40 | Immutable.js | Reason/BuckleScript 41 | ----|----- 42 | List | List 43 | Map | Map 44 | OrderedMap | [Belt Map](https://bucklescript.github.io/bucklescript/api/Belt.Map.html) 45 | Set | Set 46 | OrderedSet | [Belt Set](https://bucklescript.github.io/bucklescript/api/Belt.Map.html) 47 | Stack | List 48 | Record | Record 49 | Seq | Use `lazy` 50 | 51 | ## Contributing 52 | 53 | Prerequisite: `https://github.com/reasonml/reason-cli`. 54 | 55 | ``` 56 | git clone https://github.com/BuckleTypes/bs-immutablejs.git 57 | cd bs-immutablejs 58 | npm install 59 | npm start 60 | ``` 61 | -------------------------------------------------------------------------------- /bsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bs-immutablejs", 3 | "bsc-flags": ["-bs-super-errors"], 4 | "sources": ["src"], 5 | "refmt": 3 6 | } 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bs-immutablejs", 3 | "version": "0.1.0", 4 | "description": "Reason + BuckleScript bindings to Immutable.js", 5 | "main": "index.js", 6 | "scripts": { 7 | "build": "bsb -make-world", 8 | "start": "bsb -make-world -w", 9 | "clean": "bsb -clean-world", 10 | "test": "exit 0" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/reasonml-community/bs-immutablejs.git" 15 | }, 16 | "homepage": "https://github.com/reasonml-community/bs-immutablejs#readme", 17 | "bugs": "https://github.com/reasonml-community/bs-immutablejs/issues", 18 | "keywords": [ 19 | "reason", 20 | "bucklescript", 21 | "data", 22 | "immutable", 23 | "persistent", 24 | "data structures" 25 | ], 26 | "author": "", 27 | "license": "MIT", 28 | "devDependencies": { 29 | "bs-platform": "^3.0.0" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/immJsRe.re: -------------------------------------------------------------------------------- 1 | /*** 2 | * Copyright 2004-present Facebook. All Rights Reserved. 3 | */ 4 | module OrderedMap = { 5 | type t('key, 'value); 6 | [@bs.send] [@bs.return nullable] 7 | external get : (t('key, 'value), 'key) => option('value) = ""; 8 | [@bs.send] 9 | external set : (t('key, 'value), 'key, 'value) => t('key, 'value) = ""; 10 | [@bs.send] 11 | external filter : 12 | (t('key, 'value), ('value, 'key, t('key, 'value)) => bool) => 13 | t('key, 'value) = 14 | ""; 15 | [@bs.send] 16 | external map : 17 | (t('key, 'value), ('value, 'key, t('key, 'value)) => 'value2) => 18 | t('key, 'value2) = 19 | ""; 20 | [@bs.send] 21 | external forEach : 22 | (t('key, 'value), ('value, 'key, t('key, 'value)) => unit) => unit = 23 | ""; 24 | [@bs.send] 25 | external sort : 26 | (t('key, 'value), ('value, 'value) => int) => t('key, 'value) = 27 | ""; 28 | [@bs.send] external has : (t('key, 'value), 'key) => bool = ""; 29 | [@bs.send] 30 | external reduce : 31 | ( 32 | t('key, 'value), 33 | ('reduction, 'value, 'key, t('key, 'value)) => 'reduction, 34 | 'reduction 35 | ) => 36 | 'reduction = 37 | ""; 38 | [@bs.send] 39 | external every : 40 | (t('key, 'value), ('value, 'key, t('key, 'value)) => bool) => bool = 41 | ""; 42 | [@bs.send] external toArray : t('key, 'value) => array('value) = ""; 43 | [@bs.module "immutable"] 44 | external fromArray : array(('key, 'value)) => t('key, 'value) = 45 | "OrderedMap"; 46 | [@bs.module "immutable"] 47 | external fromDict : Js.Dict.t('value) => t(string, 'value) = "OrderedMap"; 48 | [@bs.module "immutable"] 49 | external empty : unit => t('key, 'value) = "OrderedMap"; 50 | [@bs.send] [@bs.return nullable] 51 | external first : t('key, 'value) => option('value) = ""; 52 | [@bs.send] external count : t('key, 'value) => int = ""; 53 | [@bs.get] external size : t('key, 'value) => int = ""; 54 | [@bs.send] external isEmpty : t('key, 'value) => bool = ""; 55 | }; 56 | 57 | module Set = { 58 | type t('value); 59 | [@bs.send] external includes : (t('value), 'value) => bool = ""; 60 | [@bs.send] external contains : (t('value), 'value) => bool = ""; 61 | [@bs.module "immutable"] 62 | external fromArray : array('value) => t('value) = "Set"; 63 | }; 64 | 65 | module OrderedSet = { 66 | type t('value); 67 | [@bs.send] 68 | external map : 69 | (t('value), ('value, 'value, t('value)) => 'value2) => t('value2) = 70 | ""; 71 | [@bs.module "immutable"] 72 | external fromArray : array('value) => t('value) = "OrderedSet"; 73 | [@bs.send] external toArray : t('value) => array('value) = ""; 74 | [@bs.send] external add : (t('value), 'value) => t('value) = ""; 75 | [@bs.send] external remove : (t('value), 'value) => t('value) = ""; 76 | [@bs.send] external has : (t('value), 'value) => bool = ""; 77 | [@bs.get] external size : t('value) => int = ""; 78 | [@bs.send] external first : t('value) => 'value = ""; 79 | }; 80 | 81 | module List = { 82 | type t('value); 83 | [@bs.send] external filter : (t('value), 'value => bool) => t('value) = ""; 84 | [@bs.send] 85 | external forEach : (t('value), ('value, int, t('value)) => bool) => int = 86 | ""; 87 | [@bs.send] external toArray : t('value) => array('value) = ""; 88 | [@bs.module "immutable"] 89 | external fromArray : array('value) => t('value) = "List"; 90 | [@bs.send] [@bs.return nullable] 91 | external first : t('value) => option('value) = ""; 92 | [@bs.send] external count : t('value) => int = ""; 93 | [@bs.send] external push : (t('value), 'value) => t('value) = ""; 94 | [@bs.send] external isEmpty : t('value) => bool = ""; 95 | [@bs.send] 96 | external map : 97 | (t('value), ('value, int, t('value)) => 'value2) => t('value2) = 98 | ""; 99 | }; 100 | 101 | module Seq = { 102 | type t('value); 103 | [@bs.send] external filter : (t('value), 'value => bool) => t('value) = ""; 104 | [@bs.send] external toArray : t('value) => array('value) = ""; 105 | [@bs.send] external isEmpty : t('value) => bool = ""; 106 | [@bs.send] external count : t('value) => int = ""; 107 | [@bs.send] 108 | external sort : (t('value), ('value, 'value) => int) => t('value) = ""; 109 | [@bs.module "immutable"] 110 | external fromArray : array('value) => t('value) = "Seq"; 111 | [@bs.module "immutable"] 112 | external fromList : List.t('value) => t('value) = "Seq"; 113 | [@bs.send] 114 | external slice : 115 | (t('value), ~begin_: int=?, ~end_: int=?, unit) => t('value) = 116 | ""; 117 | [@bs.send] external join : (t('value), string) => string = ""; 118 | [@bs.send] external take : (t('value), int) => t('value) = ""; 119 | [@bs.send] 120 | external map : 121 | (t('value1), ('value, int, t('value)) => 'value2) => t('value2) = 122 | ""; 123 | }; 124 | 125 | module Map = { 126 | type t('key, 'value); 127 | [@bs.send] 128 | external forEach : 129 | (t('key, 'value), ('value, 'key, t('key, 'value)) => unit) => unit = 130 | ""; 131 | [@bs.send] [@bs.return nullable] 132 | external get : (t('key, 'value), 'key) => option('value) = ""; 133 | [@bs.send] 134 | external filter : 135 | (t('key, 'value), ('value, 'key, t('key, 'value)) => bool) => 136 | t('key, 'value) = 137 | ""; 138 | [@bs.send] 139 | external map : 140 | (t('key, 'value), ('value, 'key, t('key, 'value)) => 'value2) => 141 | t('key, 'value2) = 142 | ""; 143 | [@bs.send] external count : t('key, 'value) => int = ""; 144 | [@bs.get] external size : t('key, 'value) => int = ""; 145 | [@bs.send] 146 | external reduce : 147 | ( 148 | t('key, 'value), 149 | ('reduction, 'value, 'key, t('key, 'value)) => 'reduction, 150 | 'reduction 151 | ) => 152 | 'reduction = 153 | ""; 154 | [@bs.send] 155 | external sort : 156 | (t('key, 'value), ('value, 'value) => int) => t('key, 'value) = 157 | ""; 158 | [@bs.send] external valueSeq : t('key, 'value) => Seq.t('value) = ""; 159 | }; 160 | --------------------------------------------------------------------------------