├── .eslintrc ├── .gitignore ├── .prettierignore ├── .prettierrc.json ├── .travis.yml ├── README.md ├── dist ├── fnMatch.js ├── index.d.ts ├── index.js └── test.d.ts ├── docs ├── README.md ├── css │ ├── github-light.css │ ├── mui.css │ └── styles.css ├── img │ ├── cheesy_logo.png │ ├── logo.ai │ └── pizza.svg ├── index.html ├── js │ ├── fnMatch.js │ ├── index.js │ └── viz.js └── main.js ├── examples └── test.ts ├── jest.config.ts ├── package-lock.json ├── package.json ├── spec ├── array_test.js ├── destructuring_test.js ├── function_test.js └── simple_test.js ├── src └── index.ts ├── test ├── index.html └── test.js ├── tsconfig.json └── webpack.config.js /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "jest": true 4 | }, 5 | "parserOptions": { 6 | "ecmaVersion": 6 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | node_modules 3 | .ignore 4 | .DS_Store 5 | notes -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | dist 2 | docs -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | node_js: 4 | - "16" 5 | install: 6 | - npm install 7 | script: 8 | - npm test 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |
3 |
Pattern matching without a transpiler.
73 | 74 | 92 | 93 |98 | match(value)(pattern1, pattern2, ...etc) 99 | func(pattern1, pattern2, ...etc)(value)100 | 101 |
104 | View the Project on GitHub 105 | mrkev/fnMatch 106 | 107 |
108 |109 | Hosted on GitHub Pages — Theme by 110 | orderedlist 111 | 112 |
113 | 114 |
119 | match("hello")(
120 | (_ = "hey") => {}, // doesn't match
121 | (_ = "world") => {}, // doesn't match
122 | (_ = "hello") => {}, // matches!
123 | )
124 |
125 |
126 |
129 |
130 | // Prints "undefined"
131 | match()(
132 | () => console.log('undefined'),
133 | (_) => console.log('anything'),
134 | );
135 |
136 | // Prints "anything"
137 | match(3)(
138 | () => console.log('undefined'),
139 | (_) => console.log('anything'),
140 | );
141 |
142 |
143 |
144 |
145 | // This prints nothing 151 | match([])( 152 | ([x, ...y]) => console.log(x, y) 153 | ) 154 |155 | 156 |
163 | const contacts = [
164 | {name: {first: "Ajay"}, last: "Gandhi"},
165 | {name: {first: "Seunghee", last: "Han"}},
166 | {name: "Evil Galactic Empire, Inc.", kind: "company"}
167 | ]
168 | match(contacts)(
169 | ([{kind = "company", name}, ..._]) => console.log("Found company:", name)
170 | )
171 |
172 |
174 | const contacts = [
175 | {name: {first: "Ajay"}, last: "Gandhi"},
176 | {name: {first: "Seunghee", last: "Han"}},
177 | {name: "Evil Galactic Empire, Inc.", kind: "company"}
178 | ]
179 | match(contacts)(
180 | ([{name: {first:first_person}}, ..._]) => console.log("First contact is", first_person)
181 | )
182 |
183 |
184 |
185 |
191 | // Prints "matches"
192 | match({name: "Ajay", age: 22})(
193 | ({name}) => console.log("matches"),
194 | ({name, age}) => console.log("too late!")
195 | )
196 |
197 |
198 | 200 | const a = match(3)( 201 | (_ = 3) => 4, 202 | (_) => 0, 203 | ); 204 | console.log(a) // Prints 4 205 |206 | 207 |
210 | const value = {
211 | name: 'Ajay',
212 | value: {
213 | x: 34,
214 | },
215 | };
216 |
217 | function destructNotes ({notes}) { return notes; }
218 | let destructErrors = ({ errors }) => errors;
219 | let destructResult = function ({result}) { return result; }
220 |
221 | let getResult = func(
222 | destructNotes,
223 | destructErrors,
224 | destructResult
225 | );
226 |
227 | console.log(getResult({notes: "This works!"}))
228 |
229 |
230 |
231 |