Skip to content

Commit

Permalink
slightly optimize things
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeoneWeird committed Apr 4, 2015
1 parent 45a6be4 commit 3e77e65
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 40 deletions.
5 changes: 3 additions & 2 deletions src/deleteKey.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Identifies a validator that wishes it's
// key/value obliterated from results

"use strict";

// Identifies a validator that wishes its key/value
// to be obliterated from results.

const DELETEKEY = { htDeleteKey: true };

export default DELETEKEY;
62 changes: 24 additions & 38 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
const fs = require("fs");
const path = require("path");

const clone = require('clone');
const clone = require("clone");

const merge = require("./merge");

let validators = {};
const validatorsPath = path.resolve(__dirname, "../validators");

let validatorsPath = path.resolve(__dirname, "../validators");
const files = fs.readdirSync(validatorsPath);

let files = fs.readdirSync(validatorsPath);
let validators = {};

files.forEach(function(file) {
let t = require(path.join(validatorsPath, file));
const t = require(path.join(validatorsPath, file));
validators[t.name] = makeParser(t.fn);
});

Expand All @@ -26,43 +26,29 @@ function makeParser(parserFunc, docFunc) {
// reason. Otherwise it should return a value.
// This value can be mutated, it will be the "validated" value.

return function validator(args, childValidators) {
return function validator(args = {}, childValidators = {}) {

// Overly complex method of managing argument order!
switch(arguments.length) {

case 0: {
childValidators = {};
args = {};
break;
}

case 1: {
if(arguments.length === 1) {

// One argument, are they args or validators?
let areValidators = false;
// One argument, are they args or validators?
let areValidators = false;

for(let k in arguments[0]) {
let arg = arguments[0][k];
if(typeof arg == "object" && Object.prototype.toString.call(arg) === "[object Object]") {
if(!arg.hasOwnProperty("$validators")) {
arguments[0][k] = validators.Object(arg);
}
areValidators = true;
for(let k in arguments[0]) {
let arg = arguments[0][k];
if(typeof arg == "object" && Object.prototype.toString.call(arg) === "[object Object]") {
if(!arg.hasOwnProperty("$validators")) {
arguments[0][k] = validators.Object(arg);
}
areValidators = true;
}
}

if(areValidators) {
childValidators = arguments[0];
args = {};
} else {
args = arguments[0];
childValidators = null;
}
break;

if(areValidators) {
childValidators = arguments[0];
args = {};
}

}

var parser = new Parser(parserFunc, args, childValidators, docFunc);
Expand Down Expand Up @@ -99,14 +85,14 @@ function Parser(parserFunc, args, childValidators, docFunc) {

Parser.prototype.parse = function(data, key, first) {
//All validators should handle opt (optional)
let args = merge(this.$args, { opt: false });
let val = this.$parserFunc.call(this, args, this.$validators, data, key);
const args = merge(this.$args, { opt: false });
const val = this.$parserFunc(args, this.$validators, data, key);
if(first && val !== null && typeof val == "object" && val.htDeleteKey) return null;
return val;
};

Parser.prototype.validate = function(data, key) {
return this.parse(data, key || "schema", true);
Parser.prototype.validate = function(data, key = "schema") {
return this.parse(data, key, true);
};

Parser.prototype.document = function() {
Expand Down

0 comments on commit 3e77e65

Please sign in to comment.