Skip to content

Instantly share code, notes, and snippets.

@mason276752
Last active December 27, 2017 07:40
Show Gist options
  • Save mason276752/72e53d37776db9e7860ca22749f88515 to your computer and use it in GitHub Desktop.
Save mason276752/72e53d37776db9e7860ca22749f88515 to your computer and use it in GitHub Desktop.
check type
let typeOf = (value) => {
let result = Object.prototype.toString.call(value).slice(8, -1);
if (result === 'Number') {
switch (value) {
case Infinity:
return 'Infinity';
case -Infinity:
return '-Infinity';
case value:
return 'Number';
default:
return 'NaN';
};
}
return result;
};
let whatIsIt = (value) => {
console.log(typeOf(value));
return true;
};
let isNumber = (number) => {
return typeOf(number) === 'Number';
};
let isInfinity = (infinity) => {
return typeOf(infinity) === 'Infinity';
};
let isNegativeInfinity = (negativeInfinity) => {
return typeOf(negativeInfinity) === '-Infinity';
};
let isRealNaN = (nan) => {
// isNaN(NaN) => true
// isNaN(undefined) => true
return typeOf(nan) === 'NaN';
};
let isRealFinite = (number) => {
// isFinite(6) => true
// isFinite('6') => true
// isFinite('six') => false
// isFinite(NaN) => false
// isFinite(undefined) => false
return isNumber(number) && isFinite(number);
};
let isFunction = (func) => {
return func instanceof Function;
};
let isArray = (array) => {
return array instanceof Array;
};
let isRegExp = (regExp) => {
return regExp instanceof RegExp;
};
let isString = (string) => {
return typeOf(string) === 'String';
};
let isBoolean = (boolean) => {
return typeOf(boolean) === 'Boolean';
};
let isNull = (nu) => {
return nu === null;
};
let isUndefind = (unde) => {
return unde === undefined;
};
let isJSON = (obj) => {
if (obj instanceof Object && !(obj instanceof Array) && !(obj instanceof RegExp) && !(obj instanceof Set) && !(obj instanceof Map) &&
!isFunction(obj) && !isString(obj) && !isBoolean(obj) && !isNumber(obj) && !(obj === null) && !(obj === undefined)) {
try {
if (JSON.stringify(obj) && typeOf(obj) === 'Object') {
return true;
}
whatIsIt(obj);
} catch (e) {}
}
return false;
};
let jsonFull = (template, target) => {
let keys = Object.keys(template);
for (let key of keys) {
if (isArray(template[key])) {
target[key] = target[key] || [];
if (isArray(target[key])) {
for (let nextTarget of target[key]) {
jsonFull(template[key][0], nextTarget);
}
} else {
console.error(`1.type not equal,template['${key}'] is Array ,but target['${key}'] is not.`);
}
} else if (isJSON(template[key])) {
target[key] = target[key] || {};
if (isJSON(target[key])) {
target[key] = jsonFull(template[key], target[key]);
} else {
console.error(`2.type not equal,template['${key}'] is JSON ,but target['${key}'] is not.`);
}
} else {
if (target[key] === undefined || target[key] === null) {
target[key] = null;
} else if (isJSON(target[key]) || isArray(template[key]) === true) {
console.error(`3.type not equal,template['${key}'] not Array or JSON ,but target['${key}'] is it.`);
}
}
}
return target;
};
let clearSpaceData = (json) => {
let keys = Object.keys(json);
for (let key of keys) {
if (isArray(json[key])) {
for (let nextTarget of json[key]) {
json[key] = clearSpaceData(nextTarget);
}
if (json[key].length === 0) {
delete json[key];
}
} else if (isJSON(json[key])) {
json[key] = clearSpaceData(json[key]);
if (Object.keys(json[key]).length === 0) {
delete json[key];
}
} else {
if (isUndefind(json[key]) || isNull(json[key])) {
delete json[key];
}
}
}
return json;
};
if (module || module.exports) {
module.clearSpaceData = module.exports.clearSpaceData = clearSpaceData;
module.jsonFull = module.exports.jsonFull = jsonFull;
module.typeOf = module.exports.typeOf = typeOf;
module.whatIsIt = module.exports.whatIsIt = whatIsIt;
module.isNumber = module.exports.isNumber = isNumber;
module.isInfinity = module.exports.isInfinity = isInfinity;
module.isNegativeInfinity = module.exports.isNegativeInfinity = isNegativeInfinity;
module.isRealNaN = module.exports.isRealNaN = isRealNaN;
module.isRealFinite = module.exports.isRealFinite = isRealFinite;
module.isFunction = module.exports.isFunction = isFunction;
module.isArray = module.exports.isArray = isArray;
module.isRegExp = module.exports.isRegExp = isRegExp;
module.isString = module.exports.isString = isString;
module.isBoolean = module.exports.isBoolean = isBoolean;
module.isNull = module.exports.isNull = isNull;
module.isUndefind = module.exports.isUndefind = isUndefind;
module.isJSON = module.exports.isJSON = isJSON;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment