whether-is
Version:
A comprehensive, extensible, and type-friendly type and value checking utility for JavaScript/TypeScript.
2 lines (1 loc) • 5.05 kB
JavaScript
const r=Object.is,t=Object.prototype,e=Reflect.ownKeys,n=Reflect.get,i=Reflect.getPrototypeOf,o=Array.isArray,u=Number.isNaN,s=Number.isInteger,f=Number.isSafeInteger,l=Number.isFinite,c=Number.MAX_SAFE_INTEGER,y=Number.MIN_SAFE_INTEGER,a=Function.prototype.toString,p=Symbol.iterator,b=Boolean;function h(r){let t=r;const n=new Set;for(;null!==t;)e(t).forEach(r=>n.add(r)),t=i(t);return n}function m(t,e,n){if(typeof t!=typeof e)return!1;if(r(t,e))return!0;if("object"!=typeof t)return!1;if(null===t&&null!==e)return!1;if(null===e&&null!==t)return!1;if(n(Date,t)&&n(Date,e))return t.getTime()===e.getTime();if(n(RegExp,t)&&n(RegExp,e))return t.source===e.source&&t.flags===e.flags;if(o(t)&&o(e)){if(t.length!==e.length)return!1;for(let r=0;r<t.length;r++)if(!m(t[r],e[r],n))return!1;return!0}if(n(Map,t)&&n(Map,e)){if(t.size!==e.size)return!1;for(const[r,i]of t)if(!e.has(r)||!m(i,e.get(r),n))return!1;return!0}if(n(Set,t)&&n(Set,e)){if(t.size!==e.size)return!1;for(const r of t){let t=!1;for(const i of e)if(m(r,i,n)){t=!0;break}if(!t)return!1}return!0}const i=[...h(t)],u=[...h(e)];if(i.length!==u.length)return!1;for(const r of i){if(!u.includes(r))return!1;if(!m(t[r],e[r],n))return!1}return!0}const d=Symbol("NOT_GIVEN");const v=new class extends Function{constructor(){super("o","return !!o")}is(t,e){return r(t,e)}equal(r,t){return m(r,t,this.likeInstanceOf.bind(this))}isTruthy(r){return b(r)}isFalsy(r){return!r}isEmpty(r){return!r||(o(r)?0===r.length:"object"==typeof r&&0===e(r).length)}isEmptyObject(r){return"object"!=typeof r?null:0===e(r).length}isEmptyArray(r){return o(r)?0===r.length:null}isNegativeZero(t){return r(t,-0)}isPositiveZero(t){return r(t,0)}likeInstanceOf(t,e){if(e instanceof t)return!0;if(!this.isObject(e))return!1;let i;i=r(t,Promise)?new t(()=>({})):new t;const o=[...h(i)],u=h(e);if(o.length>u.size)return!1;for(let r=0;r<o.length;r++){const t=o[r];if(!u.has(t))return!1;if(typeof n(e,t)!=typeof i[t])return!1}return!0}likeError(r){return this.likeInstanceOf(Error,r)}likeDate(r){return this.likeInstanceOf(Date,r)}likePromise(r){return this.likeInstanceOf(Promise,r)}likeSet(r){return this.likeInstanceOf(Set,r)&&this.isIterable(r)}likeMap(r){return this.likeInstanceOf(Map,r)&&this.isIterable(r)}likeWeakSet(r){return this.likeInstanceOf(WeakSet,r)}likeWeakMap(r){return this.likeInstanceOf(WeakMap,r)}isIterable(r){return null!=r&&"function"==typeof r[p]}isPlainObject(r){if(!this.isObject(r))return!1;const e=i(r);return e===t||null===e}likeRegExp(r){return this.likeInstanceOf(RegExp,r)}isPromiseLike(r){return!!this.likeObject(r)&&(r instanceof Promise||"function"==typeof r.then)}isObject(r){return"object"==typeof r&&null!==r}likeObject(r){return"object"==typeof r&&null!==r||"function"==typeof r}isFunction(r){return"function"==typeof r}isString(r){return"string"==typeof r}isNumber(r){return"number"==typeof r}isBoolean(r){return"boolean"==typeof r}isUndefined(r){return void 0===r}isNull(r){return null===r}isSymbol(r){return"symbol"==typeof r}isBigInt(r){return"bigint"==typeof r}isNullish(r){return null==r}isPrimitive(r){const t=typeof r;return"string"===t||"number"===t||"boolean"===t||"undefined"===t||"symbol"===t||"bigint"===t||null===r}isField(r){return"string"==typeof r||"symbol"==typeof r}isPropertyKey(r){return"string"==typeof r||"symbol"==typeof r||"number"==typeof r}isNaN(r){return"number"!=typeof r?null:u(r)}isInteger(r){return s(r)}isSafeInteger(r){return f(r)}isSafeNumber(r){return"number"==typeof r&&r<=c&&r>=y}isFinite(r){return l(r)}isClass(r){if("function"!=typeof r)return!1;try{new new Proxy(r,{construct:()=>({})});const t=a.call(r).replace(/\s/g,"");return t.startsWith("class")||t.startsWith("[class")}catch{return!1}}isArray(t,e=d){if(!o(t))return!1;if(r(e,d))return!0;if("function"!=typeof e)throw new Error("`predicate` must be a function");for(let r=0;r<t.length;r++){if(!1===e(t[r],r,t))return!1}return!0}isArrowFunction(r){if("function"!=typeof r)return!1;try{return new new Proxy(r,{construct:()=>({})}),!1}catch{return!0}}orNegativeZero(r){return void 0===r||this.isNegativeZero(r)}orPositiveZero(r){return void 0===r||this.isPositiveZero(r)}orFunction(r){return void 0===r||this.isFunction(r)}orObject(r){return void 0===r||this.isObject(r)}orLikeObject(r){return void 0===r||this.likeObject(r)}orString(r){return void 0===r||this.isString(r)}orNumber(r){return void 0===r||this.isNumber(r)}orBoolean(r){return void 0===r||this.isBoolean(r)}orUndefined(r){return void 0===r||this.isUndefined(r)}orNull(r){return void 0===r||this.isNull(r)}orSymbol(r){return void 0===r||this.isSymbol(r)}orBigInt(r){return void 0===r||this.isBigInt(r)}orField(r){return void 0===r||this.isField(r)}orPropertyKey(r){return void 0===r||this.isPropertyKey(r)}orNaN(r){return void 0===r||this.isNaN(r)}orInteger(r){return void 0===r||this.isInteger(r)}orSafeInteger(r){return void 0===r||this.isSafeInteger(r)}orSafeNumber(r){return void 0===r||this.isSafeNumber(r)}orClass(r){return void 0===r||this.isClass(r)}orArray(r,t=d){return void 0===r||this.isArray(r,t)}};export{v as whether};