diff --git a/lib/pampy.js b/lib/pampy.js index 3613788..df21857 100644 --- a/lib/pampy.js +++ b/lib/pampy.js @@ -3,8 +3,6 @@ const PAD_VALUE = Symbol('PadValueType'); const _ = Symbol('UnderscoreType'); const ANY = _; -const STRING = Symbol('StringType'); -const NUMBER = Symbol('NumberType'); const HEAD = Symbol('HeadType'); const TAIL = Symbol('TailType'); const REST = TAIL; @@ -36,29 +34,26 @@ function run(action, x) { function matchValue(patt, value) { - if(patt === '_') { + if (patt === '_') { // Behaves like UnderscoreType return [true, [value]]; } - else if (isValue(patt)) { - return [patt === value, []] - } else if (patt === PAD_VALUE) { return [false, []]; } - else if (patt === STRING) { - let bool = typeof(value) === 'string' || value instanceof String; - if (bool) return [bool, [value]]; - else return [false, []]; + else if (patt === String) { + let bool = typeof(value) === 'string' || value instanceof String; + if (bool) return [bool, [value]]; + else return [false, []]; } - else if (patt === NUMBER) { + else if (patt === Number) { let bool = typeof(value) === 'number' || value instanceof Number; if (bool) return [bool, [value]]; else return [false, []]; } else if (patt === Array) { if (value instanceof Array) { - return [true, [value]]; + return [true, [value]]; } else return [false, []]; } @@ -72,7 +67,11 @@ function matchValue(patt, value) { return [true, [value]]; return [false, []]; } - catch (err) {} + catch (err) { + } + } + else if (isValue(patt)) { + return [patt === value, []] } else if (patt instanceof Function) { // console.log(`[${patt}] instanceof Function`); @@ -159,14 +158,14 @@ function matchDict(patt, value) { } } if (!matchedLeftAndRight) - return [false, []]; + return [false, []]; } return [true, totalExtracted]; } function pairwise(args) { let res = []; - for (let i = 0; i < args.length; i+=2) { + for (let i = 0; i < args.length; i += 2) { res.push([args[i], args[i + 1]]); } return res; @@ -202,7 +201,7 @@ function match(x) { function matchAll(rows) { let total = []; - for(let i=0; i < rows.length; i++) { + for (let i = 0; i < rows.length; i++) { let row = rows[i]; let pairs = [...arguments].slice(1); let res = match.apply(null, [row].concat(pairs)); @@ -237,8 +236,6 @@ module.exports = { matchAll, zipLongest, PAD_VALUE, - STRING, - NUMBER, _, ANY, HEAD, diff --git a/tests/tests.js b/tests/tests.js index abed9c8..bb090db 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -4,7 +4,7 @@ let assert = require('chai').assert; let lodash = require('lodash'); let fs = require('fs'); let {matchArray, matchValue, matchDict, zipLongest, match, matchAll, _, HEAD, TAIL, REST,} = require('../lib/pampy'); -let {PAD_VALUE, STRING, NUMBER, ANY, MatchError} = require('../lib/pampy'); +let {PAD_VALUE, ANY, MatchError} = require('../lib/pampy'); describe('matchValue', () => { @@ -19,10 +19,10 @@ describe('matchValue', () => { assert.deepEqual(matchValue(_, 1), [true, [1]]) }); it('types', () => { - assert.deepEqual(matchValue(STRING, "ok"), [true, ["ok"]]); - assert.deepEqual(matchValue(STRING, 3), [false, []]); - assert.deepEqual(matchValue(NUMBER, 3), [true, [3]]); - assert.deepEqual(matchValue(NUMBER, "ok"), [false, []]); + assert.deepEqual(matchValue(String, "ok"), [true, ["ok"]]); + assert.deepEqual(matchValue(String, 3), [false, []]); + assert.deepEqual(matchValue(Number, 3), [true, [3]]); + assert.deepEqual(matchValue(Number, "ok"), [false, []]); }); }); describe('matchArray', () => { @@ -39,8 +39,8 @@ describe('matchArray', () => { assert.deepEqual(matchArray([1, _, _], [1, 2, 3]), [true, [2, 3]]); assert.deepEqual(matchArray([1, _, 3], [1, 2, 3]), [true, [2]]); - assert.deepEqual(matchArray([1, NUMBER, 3], [1, 2, 3]), [true, [2]]); - assert.deepEqual(matchArray([1, STRING, NUMBER], [1, "2", 3]), [true, ["2", 3]]); + assert.deepEqual(matchArray([1, Number, 3], [1, 2, 3]), [true, [2]]); + assert.deepEqual(matchArray([1, String, Number], [1, "2", 3]), [true, ["2", 3]]); }); it('nested', () => { assert.deepEqual(matchArray([1, [_, 3], _], [1, [2, 3], 4]), [true, [2, 4]]) @@ -59,7 +59,7 @@ describe('matchDict', () => { }); it('ambiguous double _', () => { assert.deepEqual(matchDict({a: _, _: _}, {a: 1, b: 2}), [true, [1, 'b', 2]]); - assert.deepEqual(matchDict({a: STRING, _: _}, {a: "1", b: 2}), [true, ["1", 'b', 2]]); + assert.deepEqual(matchDict({a: String, _: _}, {a: "1", b: 2}), [true, ["1", 'b', 2]]); }); }); describe('match', () => { @@ -71,10 +71,10 @@ describe('match', () => { assert.throws(() => match([1,2,3] [TAIL, 1], 1), MatchError); }); it('ignored case', () => { - assert.throws(() => match(3, STRING, "ok"), MatchError); + assert.throws(() => match(3, String, "ok"), MatchError); }); it('lambda args', () => { - assert.equal(match(3, NUMBER, (x) => x), 3); + assert.equal(match(3, Number, (x) => x), 3); assert.equal(match([1, 2], [1, _], (x) => x), 2); assert.equal(match([1, 2, 3], [_, 2, 3], (x) => x), 1); assert.deepEqual(match([1, 2, 3], [_, _, 3], (a, b) => [a, b]), [1, 2]); @@ -87,7 +87,7 @@ describe('match', () => { it('lambda cond', () => { function f(x) { return match(x, - STRING, (x => `${x} is a string`), + String, x => `${x} is a String`, x => x > 3, x => `${x} is > 3`, x => x < 3, x => `${x} is < 3`, x => x === 3, x => `${x} is = 3`); @@ -95,8 +95,8 @@ describe('match', () => { assert.equal(f(3), "3 is = 3"); assert.equal(f(2), "2 is < 3"); assert.equal(f(4), "4 is > 3"); - assert.equal(f("hello"), "hello is a string"); - assert.equal(f("3"), "3 is a string"); + assert.equal(f("hello"), "hello is a String"); + assert.equal(f("3"), "3 is a String"); }); it('fibonacci', () => { function fib(n) { @@ -169,9 +169,9 @@ describe('match', () => { return match(x, null, "null", undefined, "undefined", - NUMBER, "number"); + Number, "Number"); } - assert.equal(f(3), "number"); + assert.equal(f(3), "Number"); assert.equal(f(null), "null"); let z; assert.equal(f(z), "undefined");