Skip to content

Commit

Permalink
refactor(index): simplify, make it not so expressive
Browse files Browse the repository at this point in the history
use clean stack traces and update kind-of-extra and is-kindof

BREAKING CHANGE: simplify, now just have methods for all of the types and methods in "not" modifier

like "is.not.object(val)", "is.not.string(val)" and etc. Also has "is.string(val)" and such methods

exported
  • Loading branch information
tunnckoCore committed Nov 5, 2016
1 parent e20af51 commit 958bbac
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 7 deletions.
75 changes: 75 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,78 @@
*/

'use strict'

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

/**
* > Creates options object passed to AssertionError.
*
* @param {String} `actual` type of the actual value
* @param {String} `expected` type of the expected
* @param {String} `operator`
* @param {Any} `value` actually passed value, not it's type
* @param {Function} `fn` stack start function
* @return {Object} options object to be passed
* @api private
*/

function create (actual, expected, operator, value, fn) {
return {
actual: actual,
expected: expected,
operator: operator,
value: value,
message: actual + ' ' + operator + ' ' + expected,
stackStartFunction: fn
}
}

/**
* > Testing if `ok` not truthy, throw AssertionError,
* adds actual value to the error object and cleans the stack trace
*
* @param {Boolean} `ok`
* @param {Object} `opts`
* @return {Undefined} or throws the AssertionError
* @api private
*/
function test (ok, opts) {
if (!ok) {
var err = new assert.AssertionError(opts)
err.value = opts.value
err.stack = utils.cleanStack(err.stack)
throw err
}
}

/**
* > Creates exported methods. Such as `assert.strictEqual`, in
* the context of `assert-kindof` it is like `is.object(val)`,
* `is.number(val)`, `is.null(val)` or negative `is.not.null(val)` and
* etc.
*
* @param {String} `type` type of the given value
* @param {[type]} `control` is normal or `is.not.object` for example
* @return {Function} `fn` actual exported method to check type of some value
* @api private
*/

function check (type, control) {
return function assertIs (value) {
var actual = utils.kindOf(value)
var result = utils.is[type](value)
var operator = control === false ? '===' : '!=='
var options = create(actual, type, operator, value, assertIs)

test(result === control, options)
}
}

var assertKindof = module.exports
assertKindof.not = {}

Object.keys(utils.is).forEach(function (type) {
assertKindof[type] = check(type, true)
assertKindof.not[type] = check(type, false)
})
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "assert-kindof",
"version": "0.1.0",
"description": "wip",
"version": "1.0.1",
"description": "Check native type of value and throw AssertionError if not okey. Clean stack traces. Simplicity.",
"repository": "tunnckoCore/assert-kindof",
"author": "Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)",
"precommit.silent": true,
Expand All @@ -21,8 +21,9 @@
"commit": "git-cz"
},
"dependencies": {
"core-assert": "^0.2.1",
"clean-stacktrace": "^1.0.0",
"is-kindof": "^3.0.0",
"kind-of-extra": "^1.0.4",
"lazy-cache": "^2.0.1"
},
"devDependencies": {
Expand Down Expand Up @@ -56,4 +57,4 @@
"reflinks": true
}
}
}
}
24 changes: 23 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,29 @@
'use strict'

var test = require('mukla')
var is = require('./index')

test(function (done) {
test('should throws if not okey', function (done) {
try {
is.object(null)
} catch (err) {
test.strictEqual(err.message, 'null !== object')
test.strictEqual(err.actual, 'null')
test.strictEqual(err.expected, 'object')
}
done()
})

test('should throws if negation not okey', function (done) {
try {
is.not.number(123)
} catch (err) {
test.strictEqual(err.message, 'number === number')
}
done()
})

test('should not throw if okey', function (done) {
is.string('foo')
done()
})
8 changes: 6 additions & 2 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ require = utils // eslint-disable-line no-undef, no-native-reassign, no-global-a
* Lazily required module dependencies
*/

require('core-assert')
require('is-kindof')
require('clean-stacktrace', 'cleanStack')
require('is-kindof', 'is')
require('kind-of-extra', 'kindOf')
require = fn // eslint-disable-line no-undef, no-native-reassign, no-global-assign

// need bump in kind-of-types
delete utils.is['hybrid']

/**
* Expose `utils` modules
*/
Expand Down

0 comments on commit 958bbac

Please sign in to comment.