SimplicityJS
Pattern matching in JavaScript
Install / Use
/learn @becdetat/SimplicityJSREADME
SimplicityJS
Pattern matching in JavaScript
A port of Simplicity for C# to JavaScript.
Installation
npm install --save-dev simplicityjs
Note that the NPM package name is simplicityjs, not simplicity (which was already taken). Add a reference to ./node_modules/simplicityjs/dist/simplicity.js.
Usage
These examples are in ES6.
import match from 'simplicityjs'
var letter = 'f';
var result = match(letter)
.with('a', 'Letter is A')
.with('b', 'Letter is B')
.with(x => 'c' <= x && x <= 'h', x => 'Letter ' + x + ' is between c and h')
.else(x => 'Letter ' + x + ' is out of range')
.do();
// result = 'Letter f is between c and h'
Call it without a value and call toFunc() at the end of the method chain to get a function value that can be reused:
var formatFuzzyAmount = match()
.with(0, 'None')
.with(x => 0 < x && x <= 0.125, 'Just a bit')
.with(x => 0.125 < x && x <= 0.29, 'About one quarter')
.with(x => 0.29 < x && x <= 0.41, 'About one third')
.with(x => 0.41 < x && x <= 0.58, 'About half')
.with(x => 0.58 < x && x <= 0.7, 'About two thirds')
.with(x => 0.7 < x && x <= 0.875, 'About three quarters')
.with(x => 0.875 < x && x < 1, 'Almost all')
.else('All')
.toFunc();
var oneQuarter = formatFuzzyAmount(0.25);
var twoThirds = formatFuzzyAmount(0.66);
// oneQuarter = 'About one quarter'
// twoThirds = 'About two thirds'
Building and contributing
- Install NPM
- Clone the SimplicityJS repo
npm install
SimplicityJS's source is in ES6, transpiled to ES5 (the widely currently supported version of JavaScript in browsers) using Babel via a Gulp script. To build, install Gulp globally (sudo npm install -g gulp) and run gulp from the repo root. The default Gulp task will run Babel and write the result to ./dist/simplicity.js.
Deploying to NPN
These are instructions to myself ;-)
npm login, provide credsnpm publish
that was easy
Versions
- 3.0.1 - sigh need to
export defaultnot justexport. if only someone had written a test suite for this library. - 3.0.0 - Whoops, didn't actually build the dist js... Also removed support for distribution via Bower
- 2.0.0 - use ES6 export, don't add it to
window- breaking change - 1.0.0 - version bump because yay NPM publishing!
- 0.1.0 - first version, port of Simplicity for C#
Related Skills
node-connect
352.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
111.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
352.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
352.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
