项目作者: Leokuma

项目描述 :
Object pattern matching in JavaScript.
高级语言: JavaScript
项目地址: git://github.com/Leokuma/patswitch.js.git
创建时间: 2021-02-27T21:13:19Z
项目社区:https://github.com/Leokuma/patswitch.js

开源协议:

下载


PatswitchJS

PatswitchJS is a tiny research and implementation of object pattern matching in JavaScript. It’s inspired by Prolog’s and Rust’s pattern matching.

The primary goal of this research is to provide a usage that feels like it is a native JS feature.

Examples

Full match

  1. import Patswitch from 'patswitch.js';
  2. const obj = {a: 1, b: '2', c: 'c'};
  3. const pattern = new Patswitch(obj);
  4. switch (pattern) {
  5. case pattern({a: 1, b: 2, c: 'c'}): // false because 'b' has a different type
  6. case pattern({a: 2, b: '2'}): // false
  7. case pattern({a: 1, b: '2', c: 'c', d: 4}): // false
  8. case pattern({a: 1, b: '2', c: 'c'}): // true
  9. }

Partial match

  1. import Patswitch from 'patswitch.js';
  2. const obj = {a: 1, b: 2, c: 3};
  3. const pattern = new Patswitch(obj);
  4. switch (pattern) {
  5. case pattern({a: 1}): // true
  6. case pattern({b: 2}): // true
  7. case pattern({b: 2, c: 3}): // true
  8. case pattern({c: 3, a: 1, b: 2}): // true
  9. case pattern({a: 1, b: 2, c: 3, d: 4}): // false
  10. }

Arrays

  1. import Patswitch from 'patswitch.js';
  2. const arr = [1, 2, 3];
  3. const pattern = new Patswitch(arr);
  4. switch (pattern) {
  5. case pattern([1]): // true
  6. case pattern([2]): // false
  7. case pattern([1, ]): // true
  8. case pattern([ , 1, ]): // false
  9. case pattern([ , 2, ]): // true
  10. case pattern([ , 2]): // true
  11. case pattern([2, ]): // false
  12. case pattern([ , , , , , ]): // true
  13. case pattern([3, 2, 1]): // false
  14. case pattern([1, 2, 3, 4]): // false
  15. case pattern([1, 2, 3]): // true
  16. case pattern([1, 2, 3, , ]): // true
  17. }

Property existence

You can create a pattern to check only whether a property exists, no matter the value it holds.

You do that by assigning undefined to the property on the pattern.

  1. import Patswitch from 'patswitch.js';
  2. const obj = {a: 1, b: 2, c: 3};
  3. const pattern = new Patswitch(obj);
  4. switch (pattern) {
  5. case pattern({a: undefined}): // true. This only enforces that the 'a' prop exists, no matter its value
  6. case pattern({a: undefined, b: undefined}): // true because the props 'a' and 'b' exist
  7. case pattern({a: 1, b: undefined, c: 3}): // true
  8. case pattern({a: 1, b: 2, c: 3, d: undefined}): // false because the prop 'd' does not exist
  9. }

Nested objects

  1. const obj = {a: 'a', b: 'b', c: 'c',
  2. d: {
  3. da: 4,
  4. db: [5, 6]
  5. },
  6. e: [
  7. {ea: 7},
  8. {ea: 8}
  9. ]
  10. };
  11. const pattern = new Patswitch(obj);
  12. switch (pattern) {
  13. case pattern({d: {da: 4, db: 5}}): // false
  14. case pattern({d: {da: 4, db: [5, 40]}}): // false
  15. case pattern({d: {da: 4, db: [5, 6]}}): // true
  16. case pattern({d: {da: 4, db: [ , 6]}}): // true
  17. case pattern({d: {da: 4, db: []}}): // true
  18. case pattern({d: {da: 4}}): // true
  19. case pattern({d: undefined}): // true
  20. case pattern({e: [{ea: 7}]}): // true
  21. case pattern({e: [ , {ea: 7}]}): // false
  22. case pattern({e: [ , {ea: undefined}]}): // true
  23. case pattern({a: 'a', b: 'b', c: 'c', d: {db: [ , 6]}, e: [{ea: 7}, {ea: 8}]}): // true
  24. }