Skip to content

Instantly share code, notes, and snippets.

@funfunction
Last active July 17, 2020 07:27
Show Gist options
  • Save funfunction/b4b41804615a0ffce0c31b016de3f848 to your computer and use it in GitHub Desktop.
Save funfunction/b4b41804615a0ffce0c31b016de3f848 to your computer and use it in GitHub Desktop.
/**
@func complement
confirm that the supplied value is not an obj
@param {*} v expect anything but an obj
@return {boolean}
*/
export const isNotObj = v => !isObj(v);
/**
@func
is the value of type obj?
@notes
an empty obj is also true
@cons
may not work in IE 11
@param {{}} o obj expected
@return {boolean}
*/
export const isObj = o => isNotNil(o) && o.constructor === Object;
//@tests
const aTrue = [{}, { id: 166 },
(() => ({}))(), //a func that returns an empty obj
new Object(),
Object.create({}),
Object.fromEntries([]), //cast a 2D arr of key/value pairs into an obj
Object.fromEntries([...new Map()]), //cast a Map to an obj
JSON.parse("{}"), //deserialize a string into an obj
];
const aFalse = [null, undefined, "{}", [{}], () => { }, [], [1, 2, 3], "", Object,
new Array(), new Function(), new String(), new Number(), new Boolean(),
new RegExp(/_/), , new Date(), Infinity, BigInt, Map, new Map(),
new Map(Object.entries({ foo: "bar" })),
new Map([[1, "uno"], [2, "dos"]]),
Object.create(null),
];
logForeachParam(isObj, aTrue);
logForeachParam(isObj, aFalse);
//@perftest
timeInLoop("isObj", 1e9, //1.737 for 1e9
() => {
isObj(aTrue);
isObj(aFalse);
});
/*
logForeachParam sourcecode at:
https://gist.github.com/funfunction/42918a4751ae51828cfc4c2dd4c0678e
isNil and isNotNil sourcecode at:
https://gist.github.com/funfunction/0e64a5aa3abedfa237823d0bab2ef70b
timeInLoop sourcecode at:
https://gist.github.com/funfunction/91b5876a5f562e1e352aed0fcabc3858
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment