mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 19:37:49 +00:00
Add _.isNil function.
This commit is contained in:
committed by
John-David Dalton
parent
b135431542
commit
5644a20eec
32
lodash.js
32
lodash.js
@@ -854,7 +854,7 @@
|
|||||||
* `has`, `identity`, `includes`, `indexOf`, `inRange`, `isArguments`,
|
* `has`, `identity`, `includes`, `indexOf`, `inRange`, `isArguments`,
|
||||||
* `isArray`, `isBoolean`, `isDate`, `isElement`, `isEmpty`, `isEqual`,
|
* `isArray`, `isBoolean`, `isDate`, `isElement`, `isEmpty`, `isEqual`,
|
||||||
* `isEqualWith`, `isError`, `isFinite` `isFunction`, `isMatch`, `isMatchWith`,
|
* `isEqualWith`, `isError`, `isFinite` `isFunction`, `isMatch`, `isMatchWith`,
|
||||||
* `isNative`, `isNaN`, `isNull`, `isNumber`, `isObject`, `isPlainObject`,
|
* `isNative`, `isNaN`, `isNil`, `isNull`, `isNumber`, `isObject`, `isPlainObject`,
|
||||||
* `isRegExp`, `isString`, `isUndefined`, `isTypedArray`, `join`, `kebabCase`,
|
* `isRegExp`, `isString`, `isUndefined`, `isTypedArray`, `join`, `kebabCase`,
|
||||||
* `last`, `lastIndexOf`, `lt`, `lte`, `max`, `min`, `noConflict`, `noop`,
|
* `last`, `lastIndexOf`, `lt`, `lte`, `max`, `min`, `noConflict`, `noop`,
|
||||||
* `now`, `pad`, `padLeft`, `padRight`, `parseInt`, `pop`, `random`, `reduce`,
|
* `now`, `pad`, `padLeft`, `padRight`, `parseInt`, `pop`, `random`, `reduce`,
|
||||||
@@ -8273,6 +8273,35 @@
|
|||||||
return value === null;
|
return value === null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if `value` is `null` or `undefined`.
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @memberOf _
|
||||||
|
* @category Lang
|
||||||
|
* @param {*} value The value to check.
|
||||||
|
* @returns {boolean} Returns `true` if `value` is `null` or `undefined`, else `false`.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* _.isNil(null);
|
||||||
|
* // => true
|
||||||
|
*
|
||||||
|
* _.isNil();
|
||||||
|
* // => true
|
||||||
|
*
|
||||||
|
* _.isNil(undefined);
|
||||||
|
* // => true
|
||||||
|
*
|
||||||
|
* _.isNil(NaN);
|
||||||
|
* // => false
|
||||||
|
*
|
||||||
|
* _.isNil(void 0);
|
||||||
|
* // => false
|
||||||
|
*/
|
||||||
|
function isNil(value) {
|
||||||
|
return value == null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if `value` is classified as a `Number` primitive or object.
|
* Checks if `value` is classified as a `Number` primitive or object.
|
||||||
*
|
*
|
||||||
@@ -11515,6 +11544,7 @@
|
|||||||
lodash.isMatchWith = isMatchWith;
|
lodash.isMatchWith = isMatchWith;
|
||||||
lodash.isNaN = isNaN;
|
lodash.isNaN = isNaN;
|
||||||
lodash.isNative = isNative;
|
lodash.isNative = isNative;
|
||||||
|
lodash.isNil = isNil;
|
||||||
lodash.isNull = isNull;
|
lodash.isNull = isNull;
|
||||||
lodash.isNumber = isNumber;
|
lodash.isNumber = isNumber;
|
||||||
lodash.isObject = isObject;
|
lodash.isObject = isObject;
|
||||||
|
|||||||
55
test/test.js
55
test/test.js
@@ -7723,6 +7723,58 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
QUnit.module('lodash.isNil');
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
var args = arguments;
|
||||||
|
|
||||||
|
test('should return `true` for nulls', 1, function() {
|
||||||
|
strictEqual(_.isNil(null), true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should return `true` for undefinitions', 2, function() {
|
||||||
|
strictEqual(_.isNil(), true);
|
||||||
|
strictEqual(_.isNil(undefined), true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should return `false` for non-nils', 13, function() {
|
||||||
|
var expected = _.map(falsey, function(value) {
|
||||||
|
return undefined === value || null === value;
|
||||||
|
});
|
||||||
|
|
||||||
|
var actual = _.map(falsey, function(value) {
|
||||||
|
return _.isNil(value);
|
||||||
|
});
|
||||||
|
|
||||||
|
deepEqual(actual, expected);
|
||||||
|
|
||||||
|
strictEqual(_.isNull(args), false);
|
||||||
|
strictEqual(_.isNull([1, 2, 3]), false);
|
||||||
|
strictEqual(_.isNull(true), false);
|
||||||
|
strictEqual(_.isNull(new Date), false);
|
||||||
|
strictEqual(_.isNull(new Error), false);
|
||||||
|
strictEqual(_.isNull(_), false);
|
||||||
|
strictEqual(_.isNull(slice), false);
|
||||||
|
strictEqual(_.isNull({ 'a': 1 }), false);
|
||||||
|
strictEqual(_.isNull(1), false);
|
||||||
|
strictEqual(_.isNull(NaN), false);
|
||||||
|
strictEqual(_.isNull(/x/), false);
|
||||||
|
strictEqual(_.isNull('a'), false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should work with nulls from another realm', 2, function() {
|
||||||
|
if (_._object) {
|
||||||
|
strictEqual(_.isNil(_._null), true);
|
||||||
|
strictEqual(_.isNil(_._undefined), true);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
skipTest();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}(1, 2, 3));
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('lodash.isNumber');
|
QUnit.module('lodash.isNumber');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@@ -17043,6 +17095,7 @@
|
|||||||
'isFinite',
|
'isFinite',
|
||||||
'isFunction',
|
'isFunction',
|
||||||
'isNaN',
|
'isNaN',
|
||||||
|
'isNil',
|
||||||
'isNull',
|
'isNull',
|
||||||
'isNumber',
|
'isNumber',
|
||||||
'isObject',
|
'isObject',
|
||||||
@@ -17269,7 +17322,7 @@
|
|||||||
|
|
||||||
var acceptFalsey = _.difference(allMethods, rejectFalsey);
|
var acceptFalsey = _.difference(allMethods, rejectFalsey);
|
||||||
|
|
||||||
test('should accept falsey arguments', 223, function() {
|
test('should accept falsey arguments', 224, function() {
|
||||||
var emptyArrays = _.map(falsey, _.constant([]));
|
var emptyArrays = _.map(falsey, _.constant([]));
|
||||||
|
|
||||||
_.each(acceptFalsey, function(methodName) {
|
_.each(acceptFalsey, function(methodName) {
|
||||||
|
|||||||
Reference in New Issue
Block a user