├── .prettierrc.json ├── .prettierignore ├── dist ├── test.d.ts ├── index.d.ts └── index.js ├── .gitignore ├── docs ├── README.md ├── img │ ├── logo.ai │ ├── cheesy_logo.png │ └── pizza.svg ├── main.js ├── css │ ├── mui.css │ ├── github-light.css │ └── styles.css └── index.html ├── .eslintrc ├── .travis.yml ├── spec ├── simple_test.js ├── function_test.js ├── destructuring_test.js └── array_test.js ├── jest.config.ts ├── test ├── index.html └── test.js ├── tsconfig.json ├── package.json ├── webpack.config.js ├── examples └── test.ts ├── README.md └── src └── index.ts /.prettierrc.json: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | dist 2 | docs -------------------------------------------------------------------------------- /dist/test.d.ts: -------------------------------------------------------------------------------- 1 | export {}; 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | node_modules 3 | .ignore 4 | .DS_Store 5 | notes -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # fsmgen 2 | 3 | Generates fsm diagrams from a simple language. 4 | -------------------------------------------------------------------------------- /docs/img/logo.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrkev/fnMatch/HEAD/docs/img/logo.ai -------------------------------------------------------------------------------- /docs/img/cheesy_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrkev/fnMatch/HEAD/docs/img/cheesy_logo.png -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "jest": true 4 | }, 5 | "parserOptions": { 6 | "ecmaVersion": 6 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /spec/simple_test.js: -------------------------------------------------------------------------------- 1 | const { func } = require("../src"); 2 | 3 | const simple = func( 4 | () => "undefined", 5 | (_) => "anything" 6 | ); 7 | 8 | test("undefined", () => { 9 | expect(simple()).toBe("undefined"); 10 | }); 11 | 12 | test("anything", () => { 13 | expect(simple(3)).toBe("anything"); 14 | }); 15 | -------------------------------------------------------------------------------- /jest.config.ts: -------------------------------------------------------------------------------- 1 | // jest.config.ts 2 | import type { Config } from "@jest/types"; 3 | 4 | const config: Config.InitialOptions = { 5 | preset: "ts-jest", 6 | testEnvironment: "node", 7 | verbose: true, 8 | testRegex: "./spec/.*.(js|ts)$", 9 | rootDir: ".", 10 | // automock: true, 11 | }; 12 | 13 | export default config; 14 | -------------------------------------------------------------------------------- /test/index.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 | 6 |
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 |