Skip to content

Instantly share code, notes, and snippets.

@dfkaye
Last active May 24, 2024 19:28
Show Gist options
  • Save dfkaye/f08a4504476dcc8d0881b30944f7274f to your computer and use it in GitHub Desktop.
Save dfkaye/f08a4504476dcc8d0881b30944f7274f to your computer and use it in GitHub Desktop.
// 8 March 2024
// check that given value contains no values or is an empty array, string, object...
// from simplegraph repo 2014
// https://github.com/dfkaye/simplegraph/
// simpler than the is-empty.js gist 5 Jan 2018
// https://gist.github.com/dfkaye/f87272098c4e19e4d55a6a18515aefce
// 24 May 2024
// added non-empty prototype case,
// added comparison test file for empty() and isEmpty()
function empty(o) {
for (var k in Object(o)) {
return false;
}
return true;
}
/* test it out */
console.log(
empty(''), // true
empty(' '), // false - has an index { 0 : ' ' }
empty({}), // true
empty({ a: '' }), // false
'\narrays\n',
empty([]), // true
empty([ '' ]), // false - has an index { 0 : '' }
empty(Array(1)), // true - because slot is [empty] (not undefined or even null)
empty(Array(1).fill(1)), // false - slot has a value
empty(Array(1).fill()), // false - slot has been assigned `undefined`
'\nnumber and boolean\n',
empty( 1 ), // true - has no own properties
empty( 0 ), // true - has no own properties
empty(true), // true - has no own properties
empty(false), // true - has no own properties
'\nundefined and null\n',
empty(), // true - not "empty" but has no value
empty(null), // true - not "empty" but has no value
'\nNaN\n',
empty(NaN), // true
// added 24 may 2024
'\nObject.create\n',
empty(Object.create({p: 42})) // should be false
);
// 24 May 2024
// console tests for empty and isEmpty to show that empty() reports false for an
// object with an enumerable prototype property whereas isEmpty() reports true for
// such an object if it has no "own" properties.
+(function () {
console.group("empty");
function empty(o) {
for (var k in Object(o)) {
return false;
}
return true;
}
/* test it out */
console.log(
empty(''), // true
empty(' '), // false - has an index { 0 : ' ' }
empty({}), // true
empty({ a: '' }), // false
'\narrays\n',
empty([]), // true
empty([ '' ]), // false - has an index { 0 : '' }
empty(Array(1)), // true - because slot is [empty] (not undefined or even null)
empty(Array(1).fill(1)), // false - slot has a value
empty(Array(1).fill()), // false - slot has been assigned `undefined`
'\nnumber and boolean\n',
empty( 1 ), // true - has no own properties
empty( 0 ), // true - has no own properties
empty(true), // true - has no own properties
empty(false), // true - has no own properties
'\nundefined and null\n',
empty(), // true - not "empty" but has no value
empty(null), // true - not "empty" but has no value
'\nNaN\n',
empty(NaN), // true
'\nObject.create\n',
empty(Object.create({p: 42})) // should be false
);
console.groupEnd("empty");
})()
+(function () {
console.group("isEmpty");
function isEmpty(value) {
return Object.values(Object(value)).length == 0
}
/* test it out */
console.log(
isEmpty(''), // true
isEmpty(' '), // false - has an index { 0 : ' ' }
isEmpty({}), // true
isEmpty({ a: '' }), // false
'\narrays\n',
isEmpty([]), // true
isEmpty([ '' ]), // false - has an index { 0 : '' }
isEmpty(Array(1)), // true - because slot is [empty] (not undefined or even null)
isEmpty(Array(1).fill(1)), // false - slot has a value
isEmpty(Array(1).fill()), // false - slot has been assigned `undefined`
'\nnumber and boolean\n',
isEmpty( 1 ), // true - has no own properties
isEmpty( 0 ), // true - has no own properties
isEmpty(true), // true - has no own properties
isEmpty(false), // true - has no own properties
'\nundefined and null\n',
isEmpty(), // true - not "empty" but has no value
isEmpty(null), // true - not "empty" but has no value
'\nNaN\n',
isEmpty(NaN), // ture
'\nObject.create\n',
isEmpty(Object.create({p: 42})) // true - but should it be...?
);
console.groupEnd("isEmpty");
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment