From 319982a09c351c572fd671f6304f959b6aff8d72 Mon Sep 17 00:00:00 2001 From: Darren Scerri Date: Fri, 10 Nov 2017 19:11:35 +0100 Subject: [PATCH] Add is.boundFunction() (#31) --- readme.md | 15 +++++++++++++++ source/index.ts | 1 + source/tests/test.ts | 13 ++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index effcd76..0e52ee0 100644 --- a/readme.md +++ b/readme.md @@ -96,6 +96,21 @@ is.asyncFunction(() => {}); // => false ``` +##### .boundFunction(value) + +Returns `true` for any `bound` function. + +```js +is.boundFunction(() => {}); +// => true + +is.boundFunction(function () {}.bind(null)); +// => true + +is.boundFunction(function () {}); +// => false +``` + ##### .map(value) ##### .set(value) ##### .weakMap(value) diff --git a/source/index.ts b/source/index.ts index 1b7cf38..4da25b1 100644 --- a/source/index.ts +++ b/source/index.ts @@ -95,6 +95,7 @@ namespace is { // tslint:disable-line:no-namespace export const generatorFunction = isFunctionOfType('GeneratorFunction'); export const asyncFunction = isFunctionOfType('AsyncFunction'); + export const boundFunction = (value: any) => function_(value) && !value.hasOwnProperty('prototype'); export const regExp = isObjectOfType('RegExp'); export const date = isObjectOfType('Date'); diff --git a/source/tests/test.ts b/source/tests/test.ts index 9b350c5..a24f535 100644 --- a/source/tests/test.ts +++ b/source/tests/test.ts @@ -150,6 +150,13 @@ const types = new Map([ async () => {} // tslint:disable-line:no-empty ] }], + ['boundFunction', { + is: m.boundFunction, + fixtures: [ + () => {}, // tslint:disable-line:no-empty + function () {}.bind(null), // tslint:disable-line:no-empty only-arrow-functions + ] + }], ['map', { is: m.map, fixtures: [ @@ -362,7 +369,11 @@ test('is.array', t => { }); test('is.function', t => { - testType(t, 'function', ['generatorFunction', 'asyncFunction']); + testType(t, 'function', ['generatorFunction', 'asyncFunction', 'boundFunction']); +}); + +test('is.boundFunction', t => { + t.false(m.boundFunction(function () {})); // tslint:disable-line:no-empty only-arrow-functions }); test('is.buffer', t => {