Skip to content

Commit

Permalink
feat(src): refactored most of the source
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Apr 6, 2016
1 parent 57acf68 commit 25f5276
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 242 deletions.
239 changes: 101 additions & 138 deletions dist/check-more-types.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/check-more-types.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
"node-test": "node test/load-under-node-test.js",
"pack": "webpack",
"pkgfiles": "pkgfiles",
"pretest": "npm run format && npm run lint",
"pretest": "npm run format && npm run lint && npm run build",
"semantic-release": "semantic-release pre && npm run set-version && npm publish && semantic-release post",
"set-version": "grunt version && grunt",
"size": "tarball=\"$(npm pack .)\"; wc -c \"${tarball}\"; tar tvf \"${tarball}\"; rm \"${tarball}\";",
Expand Down
5 changes: 4 additions & 1 deletion src/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

var low = require('./low-level')
var logic = require('./logic')
var verify = require('./verify')

verify(low.fn(low.isArray), 'missing low level isArray')

/**
Returns true if the argument is an array with at least one value
Expand Down Expand Up @@ -41,7 +44,7 @@ Returns true if given array only has strings
@param checkLowerCase Checks if all strings are lowercase
*/
function arrayOfStrings (a, checkLowerCase) {
var v = low.isArray(a) && a.every(low.isString)
var v = low.isArray(a) && a.every(low.string)
if (v && low.bool(checkLowerCase) && checkLowerCase) {
return a.every(low.lowerCase)
}
Expand Down
4 changes: 2 additions & 2 deletions src/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var shaReg = /^[0-9a-f]{40}$/
@method commitId
*/
function commitId (id) {
return low.isString(id) &&
return low.string(id) &&
id.length === 40 &&
shaReg.test(id)
}
Expand All @@ -46,7 +46,7 @@ var shortShaReg = /^[0-9a-f]{7}$/
@method shortCommitId
*/
function shortCommitId (id) {
return low.isString(id) &&
return low.string(id) &&
id.length === 7 &&
shortShaReg.test(id)
}
Expand Down
61 changes: 8 additions & 53 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ if (typeof Function.prototype.bind !== 'function') {
throw new Error('Missing Function.prototype.bind, please load es5-shim first')
}

var curry2 = require('./utils').curry2
var low = require('./low-level')
var mid = require('./mid-level')
var arrays = require('./arrays')
Expand Down Expand Up @@ -75,13 +74,13 @@ if (!check.mixin) {
/** Adds new predicate to all objects
@method mixin */
check.mixin = function mixin (fn, name) {
if (low.isString(fn) && low.isFn(name)) {
if (low.string(fn) && low.fn(name)) {
var tmp = fn
fn = name
name = tmp
}

if (!low.isFn(fn)) {
if (!low.fn(fn)) {
throw new Error('expected predicate function for name ' + name)
}
if (!low.unemptyString(name)) {
Expand All @@ -92,13 +91,13 @@ if (!check.mixin) {
}

function registerPredicate (obj, name, fn) {
if (!low.isObject(obj)) {
if (!low.object(obj)) {
throw new Error('missing object ' + obj)
}
if (!low.unemptyString(name)) {
throw new Error('missing name')
}
if (!low.isFn(fn)) {
if (!low.fn(fn)) {
throw new Error('missing function')
}

Expand Down Expand Up @@ -148,11 +147,11 @@ if (!check.then) {
}

var promiseSchema = {
then: low.isFn
then: low.fn
}

// work around reserved keywords checks
promiseSchema['catch'] = low.isFn
promiseSchema['catch'] = low.fn

var hasPromiseApi = schema.schema.bind(null, promiseSchema)

Expand All @@ -164,54 +163,10 @@ function isPromise (p) {
return check.object(p) && hasPromiseApi(p)
}

// TODO just mix in all low and mid level predicates
// new predicates to be added to check object. Use object to preserve names
var predicates = {
nulled: low.isNull,
fn: low.isFn,
string: low.isString,
unemptyString: low.unemptyString,
object: low.isObject,
number: low.isNumber,
array: Array.isArray,
positiveNumber: low.positiveNumber,
negativeNumber: low.negativeNumber,
// a couple of aliases
positive: low.positiveNumber,
negative: low.negativeNumber,
defined: low.defined,
same: low.same,
allSame: mid.allSame,
bit: low.bit,
bool: low.bool,
has: low.has,
lowerCase: low.lowerCase,
raises: mid.raises,
empty: low.empty,
found: mid.found,
emptyString: low.emptyString,
unempty: low.unempty,
unit: mid.unit,
hexRgb: mid.hexRgb,
sameLength: mid.sameLength,
index: mid.index,
oneOf: curry2(mid.oneOf, true),
promise: isPromise,
validDate: low.validDate,
equal: curry2(low.equal),
primitive: low.primitive,
zero: low.zero,
date: low.isDate,
regexp: low.isRegExp,
instance: low.instance,
emptyObject: low.isEmptyObject,
length: curry2(low.hasLength),
floatNumber: low.isFloat,
intNumber: low.isInteger,
startsWith: mid.startsWith,
contains: mid.contains,
error: low.isError,
type: curry2(mid.type)
promise: isPromise
}

function mixCollection (collection) {
Expand All @@ -220,7 +175,7 @@ function mixCollection (collection) {
})
}

[predicates, git, internet, arrays, logic, schema].forEach(mixCollection)
[low, mid, predicates, git, internet, arrays, logic, schema].forEach(mixCollection)

check.VERSION = '{{ packageVersion }}'

Expand Down
8 changes: 4 additions & 4 deletions src/internet.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ var startsWithHttp = mid.startsWith.bind(null, 'http://')
var startsWithHttps = mid.startsWith.bind(null, 'https://')

function http (x) {
return low.isString(x) && startsWithHttp(x)
return low.string(x) && startsWithHttp(x)
}

function https (x) {
return low.isString(x) && startsWithHttps(x)
return low.string(x) && startsWithHttps(x)
}

function webUrl (x) {
return low.isString(x) &&
return low.string(x) &&
(startsWithHttp(x) || startsWithHttps(x))
}

Expand All @@ -36,7 +36,7 @@ function isUserPortNumber (x) {
@method email
*/
function email (s) {
return low.isString(s) &&
return low.string(s) &&
/^.+@.+\..+$/.test(s)
}

Expand Down
10 changes: 5 additions & 5 deletions src/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function or () {
var values = Array.prototype.slice.call(arguments, 0)
return predicates.some(function (predicate) {
try {
return low.isFn(predicate) ? predicate.apply(null, values) : Boolean(predicate)
return low.fn(predicate) ? predicate.apply(null, values) : Boolean(predicate)
} catch (err) {
// treat exceptions as false
return false
Expand All @@ -38,7 +38,7 @@ function and () {
return function orCheck () {
var values = Array.prototype.slice.call(arguments, 0)
return predicates.every(function (predicate) {
return low.isFn(predicate) ? predicate.apply(null, values) : Boolean(predicate)
return low.fn(predicate) ? predicate.apply(null, values) : Boolean(predicate)
})
}
}
Expand All @@ -61,7 +61,7 @@ function every (predicateResults) {
if (predicateResults.hasOwnProperty(property)) {
value = predicateResults[property]

if (low.isObject(value) && every(value) === false) {
if (low.object(value) && every(value) === false) {
return false
}

Expand All @@ -81,9 +81,9 @@ function map (things, predicates) {
if (predicates.hasOwnProperty(property)) {
predicate = predicates[property]

if (low.isFn(predicate)) {
if (low.fn(predicate)) {
result[property] = predicate(things[property])
} else if (low.isObject(predicate)) {
} else if (low.object(predicate)) {
result[property] = map(things[property], predicate)
}
}
Expand Down
52 changes: 28 additions & 24 deletions src/low-level.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

var curry2 = require('./utils').curry2

// low level predicates

// most of the old methods same as check-types.js
Expand Down Expand Up @@ -202,34 +204,36 @@ function equal (a, b) {
}

module.exports = {
isFn: isFn,
isString: isString,
isObject: isObject,
isNull: isNull,
unemptyString: unemptyString,
isEmptyObject: isEmptyObject,
isInteger: isInteger,
isFloat: isFloat,
positiveNumber: positiveNumber,
negativeNumber: negativeNumber,
isRegExp: isRegExp,
isError: isError,
instance: instance,
hasLength: hasLength,
isNumber: isNumber,
isDate: isDate,
defined: defined,
validDate: validDate,
primitive: primitive,
zero: zero,
same: same,
bit: bit,
bool: bool,
date: isDate,
defined: defined,
empty: empty,
emptyObject: isEmptyObject,
emptyString: emptyString,
equal: curry2(equal),
error: isError,
floatNumber: isFloat,
fn: isFn,
has: has,
instance: instance,
intNumber: isInteger,
isArray: isArray,
length: curry2(hasLength),
lowerCase: lowerCase,
empty: empty,
emptyString: emptyString,
negative: negativeNumber,
negativeNumber: negativeNumber,
nulled: isNull,
number: isNumber,
object: isObject,
positive: positiveNumber,
positiveNumber: positiveNumber,
primitive: primitive,
regexp: isRegExp,
same: same,
string: isString,
unempty: unempty,
equal: equal
unemptyString: unemptyString,
validDate: validDate,
zero: zero
}
15 changes: 8 additions & 7 deletions src/mid-level.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var low = require('./low-level')
var verify = require('./verify')
var curry2 = require('./utils').curry2

/**
Checks if the given index is valid in an array or string or -1
Expand All @@ -13,8 +14,8 @@ function found (index) {
}

function startsWith (prefix, x) {
return low.isString(prefix) &&
low.isString(x) &&
return low.string(prefix) &&
low.string(x) &&
x.indexOf(prefix) === 0
}

Expand Down Expand Up @@ -103,7 +104,7 @@ function oneOf (arr, x) {
@method unit
*/
function unit (value) {
return low.isNumber(value) &&
return low.number(value) &&
value >= 0.0 && value <= 1.0
}

Expand All @@ -113,7 +114,7 @@ var rgb = /^#(?:[0-9a-fA-F]{3}){1,2}$/
@method hexRgb
*/
function hexRgb (value) {
return low.isString(value) &&
return low.string(value) &&
rgb.test(value)
}

Expand All @@ -122,7 +123,7 @@ function hexRgb (value) {
@method raises
*/
function raises (fn, errorValidator) {
verify(low.isFn(fn), 'expected function that raises')
verify(low.fn(fn), 'expected function that raises')
try {
fn()
} catch (err) {
Expand All @@ -142,9 +143,9 @@ module.exports = {
found: found,
startsWith: startsWith,
contains: contains,
type: type,
type: curry2(type),
index: index,
oneOf: oneOf,
oneOf: curry2(oneOf, true),
sameLength: sameLength,
allSame: allSame,
unit: unit,
Expand Down
12 changes: 6 additions & 6 deletions src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ var verify = require('./verify')
var every = require('./logic').every
var map = require('./logic').map

verify(low.fn(every), 'missing check.every method')
verify(low.fn(map), 'missing check.map method')

/**
Checks if object passes all rules in predicates.
Expand All @@ -21,14 +24,11 @@ var map = require('./logic').map
@returns true or false
*/
function all (obj, predicates) {
// verify.fn(low.isFn(check.every, 'missing check.every method')
// check.verify.fn(check.map, 'missing check.map method')

verify(low.isObject(obj), 'missing object to check')
verify(low.isObject(predicates), 'missing predicates object')
verify(low.object(obj), 'missing object to check')
verify(low.object(predicates), 'missing predicates object')

Object.keys(predicates).forEach(function (property) {
if (!low.isFn(predicates[property])) {
if (!low.fn(predicates[property])) {
throw new Error('not a predicate function for ' + property + ' but ' + predicates[property])
}
})
Expand Down

0 comments on commit 25f5276

Please sign in to comment.