Skip to content

Commit 8409e3b

Browse files
Merge pull request #9 from z-pattern-matching/7-match-string
resolves #7 match string
2 parents bf5cc00 + 7f5b23c commit 8409e3b

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "z",
3-
"version": "0.0.3",
3+
"version": "0.0.6",
44
"main": "src/z.js",
55
"description": "pattern matching for javascript",
66
"scripts": {

src/z.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function getParametersWithDefaultValues (f) {
3939

4040
// create array from string for matching
4141
String.prototype.matches = function () {
42-
return this.split('').matches.apply(this, arguments)
42+
return [this].matches.apply(this, arguments)
4343
}
4444

4545
// create simple array from a number for matching
@@ -54,10 +54,19 @@ Array.prototype.matches = function () {
5454
// cycle through matches
5555
for (var i = 0; i < arguments.length && arguments[i]; i++) {
5656
var currentPattern = getParametersWithDefaultValues(arguments[i])
57+
5758
// empty array
5859
if (currentPattern.arguments.length === 0 && list.length === 0) {
5960
return currentPattern.function()
6061
}
62+
63+
// string
64+
if (typeof list === 'string' || list instanceof String) {
65+
if (currentPattern.arguments[0].value === list.toString()) {
66+
return currentPattern.function(list)
67+
}
68+
}
69+
6170
// single item array
6271
if (currentPattern.arguments.length === 1 && list.length === 1) {
6372
if (currentPattern.arguments[0].value === PATTERN_WITHOUT_VALUE || list[0] === currentPattern.arguments[0].value) {

tests/es6.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,29 @@ describe('tests with ES6', function () {
8484
myReverse([1, 2, 3, 4, 5]).should.eql([5, 4, 3, 2, 1])
8585
})
8686

87+
it.skip('should match array of arrays', function () {
88+
var matched = false;
89+
90+
[1, 2, [3]].matches(
91+
(x = Array) => { matched = true },
92+
(x) => { console.log('here', x) }
93+
)
94+
95+
matched.should.eql(true)
96+
})
97+
8798
it('should match a number', function () {
8899
(1).matches(
89-
(x) => true
100+
(x) => true
101+
).should.equal(true)
102+
})
103+
104+
it('should match a string', function () {
105+
'test'.matches(
106+
(x = 'testa') => false,
107+
(x = 'test') => true,
108+
(x = 'testo') => false,
109+
function otherwise () { return false }
90110
).should.equal(true)
91111
})
92112
})

0 commit comments

Comments
 (0)