This repository has been archived by the owner on Dec 19, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to prototype-based approach for better performance (function reuse)
Also opens the possibility of having extensible matchers, e.g. exposing JSON Schema.
- Loading branch information
Showing
47 changed files
with
902 additions
and
856 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
env: | ||
node: true | ||
mocha: true | ||
|
||
rules: | ||
comma-style: [2, "last"] | ||
default-case: 2 | ||
func-style: [2, "declaration"] | ||
guard-for-in: 2 | ||
no-floating-decimal: 2 | ||
no-nested-ternary: 2 | ||
no-undefined: 2 | ||
radix: 2 | ||
space-after-keywords: [2, "always"] | ||
space-before-blocks: 2 | ||
spaced-line-comment: [2, "always", { exceptions: ["-"]}] | ||
strict: [2, "global"] | ||
valid-jsdoc: [2, { prefer: { "return": "returns"}}] | ||
wrap-iife: 2 | ||
quotes: "single" | ||
strict: false | ||
This comment has been minimized.
Sorry, something went wrong. |
||
new-cap: false | ||
no-multi-spaces: false | ||
curly: true | ||
guard-for-in: false | ||
no-underscore-dangle: false | ||
no-new: false | ||
no-wrap-func: false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
var util = require('util'); | ||
var index = require('./index'); | ||
var Matcher = require('./matcher'); | ||
|
||
exports.spec = function compile(spec) { | ||
var matcher = null; | ||
if (spec instanceof Matcher) { | ||
matcher = spec; | ||
} else if (util.isArray(spec)) { | ||
matcher = new index.matchers.array(spec[0]); | ||
} else if (spec instanceof RegExp) { | ||
matcher = new index.matchers.regex(spec); | ||
} else if (typeof spec === 'object') { | ||
matcher = new index.matchers.object(spec); | ||
} else if (typeof spec === 'string') { | ||
matcher = new index.matchers[spec](); | ||
} | ||
if (!matcher) { | ||
throw new Error('Invalid matcher: ' + spec); | ||
} | ||
return matcher; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var inherits = require('util').inherits; | ||
var Matcher = require('./matcher.js'); | ||
|
||
function matcherFactory(define) { | ||
function M(opts) { | ||
if (this instanceof M === false) { | ||
return new M(opts); | ||
} | ||
Matcher.call(this, opts); | ||
define.initialize.call(this, opts); | ||
} | ||
|
||
inherits(M, Matcher); | ||
|
||
M.prototype._match = define.match; | ||
|
||
return M; | ||
} | ||
|
||
module.exports = matcherFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1 @@ | ||
var path = require('path'); | ||
var all = require('require-all'); | ||
var s = require('./s'); | ||
|
||
// Mount all matchers | ||
var matchers = all(path.join(__dirname, 'matchers')); | ||
for (name in matchers) { | ||
s[name] = matchers[name]; | ||
} | ||
|
||
module.exports = s; | ||
module.exports = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
function Matcher(opts) { | ||
this.optional = opts && (opts.optional === true); | ||
} | ||
|
||
function missing(value) { | ||
return value === null || typeof value === 'undefined'; | ||
} | ||
|
||
Matcher.prototype.match = function(path, value) { | ||
if (arguments.length === 1) { | ||
value = path; | ||
path = ''; | ||
} | ||
if (this.optional && missing(value)) return []; | ||
var errors = this._match(path, value); | ||
if (!errors) return []; | ||
if (typeof errors === 'string') return [{path: path, value: value, message: errors}]; | ||
else return errors; | ||
}; | ||
|
||
Matcher.prototype._match = function() {}; | ||
|
||
module.exports = Matcher; |
Oops, something went wrong.
@rprieto Hey, my build is now failing because of duplicate keys. There's a
strict
a few lines above this one.https://travis-ci.org/TabDigital/strummer/builds/127983560 for reference