esfunctional
Advanced tools
Comparing version 1.0.8 to 1.0.9
'use strict'; | ||
let chalk = require('chalk'); | ||
// es6 functional helpers globals | ||
@@ -457,1 +459,93 @@ | ||
global.byId = {_id: 1}; | ||
// test helper -- lacks | ||
let mustHaveOut = (prefix, color, indent, key, value, comment) => ( | ||
chalk.bold[color](prefix) + | ||
' ' + | ||
chalk[color]( | ||
new Array(indent + 1).join(' ') + | ||
(key == null ? '' : JSON.stringify(key) + ' : ') + | ||
value | ||
) + | ||
(comment ? chalk.bold.yellow(' // ') + chalk.yellow(comment) : '') + '\n' | ||
); | ||
let mustHaveIter = function(that, key, indent) { | ||
//jshint maxcomplexity: 13 | ||
let typeOf = (value) => ( | ||
value === undefined ? 'undefined' : | ||
value === null ? 'null' : | ||
value instanceof Function ? value.name : | ||
value.constructor.name | ||
); | ||
let typeOfThis = typeOf(this); | ||
let typeOfThat = typeOf(that); | ||
indent |= 0; | ||
if (typeOfThis !== typeOfThat) { | ||
return ( | ||
mustHaveOut('+', 'green', indent, key, typeOfThat, 'should be of type, ...') + | ||
mustHaveOut('-', 'red', indent, key, typeOfThis, '... but is of type.') | ||
); | ||
} | ||
if (that instanceof Function) return ''; | ||
let isArray = typeOfThis === 'Array'; | ||
if (!isArray && typeOfThis !== 'Object') return this === that ? '' : ( | ||
mustHaveOut('+', 'green', indent, key, JSON.stringify(that), 'should be value, ...') + | ||
mustHaveOut('-', 'red', indent, key, JSON.stringify(this), '... but is value.') | ||
); | ||
let out = ''; | ||
let hasError = false; | ||
for (let subkey in that) { | ||
let outkey = isArray ? null : subkey; | ||
if (that[subkey] === undefined && (subkey in this)) { | ||
out += mustHaveOut('-', 'red', indent + 1, outkey, typeOf(this[subkey]), 'should not exist, but is of type.'); | ||
hasError = true; | ||
} else if (that[subkey] !== undefined && !(subkey in this)) { | ||
out += mustHaveOut('+', 'green', indent + 1, outkey, typeOf(that[subkey]), 'should be of type, but not exist.'); | ||
hasError = true; | ||
} else { | ||
let line = mustHaveIter.call(this[subkey], that[subkey], outkey, indent + 1); | ||
if (line) { | ||
out += line; | ||
hasError = true; | ||
} else { | ||
out += mustHaveOut('.', 'gray', indent + 1, outkey, typeOf(that[subkey])); | ||
} | ||
} | ||
} | ||
if (hasError) { | ||
if (isArray) return ( | ||
mustHaveOut('\\', 'cyan', indent, key, '[') + out + mustHaveOut('/', 'cyan', indent, null, ']') | ||
); | ||
return mustHaveOut('\\', 'cyan', indent, key, '{') + out + mustHaveOut('/', 'cyan', indent, null, '}'); | ||
} | ||
return ''; | ||
}; | ||
global.lacks = Symbol('lacks'); | ||
Object.prototype[global.lacks] = function(that) { | ||
let out = mustHaveIter.call(this, that); | ||
if (out) { | ||
console.log(out); | ||
return true; | ||
} | ||
return false; | ||
}; |
{ | ||
"name": "esfunctional", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "Next Generation EcmaScript Functional Helpers", | ||
@@ -26,4 +26,5 @@ "main": "lib/v1/index.js", | ||
"dependencies": { | ||
"chalk": "1.1.1", | ||
"lodash": "3.10.1" | ||
} | ||
} |
18147
452
2
+ Addedchalk@1.1.1
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedchalk@1.1.1(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)