var s = 'String';var a = [1,2,3];var o = {key: 'val'};
(s.constructor === String) && console.log('its a string');(a.constructor === Array) && console.log('its an array');(o.constructor === Object) && console.log('its an object');(o.constructor === Number || s.constructor === Boolean) && console.log('this won\'t run');
this.id = "998"; // use a number or a string-equivalentfunction get(id) {if (!id || !id.toString) return;if (id.toString() === this.id.toString()) http( id || +this.id );// if (+id === +this.id) ...;}
var a = new String('')var b = ''var c = []
function isString(x) {return x !== null && x !== undefined && x.constructor === String}
console.log(isString(a))console.log(isString(b))console.log(isString(c))
const fmt = JSON.stringify
function test1() {const a = "1"const b = 1console.log(`Number(${fmt(a)}) === ${fmt(b)}`, Number(a) === b) // true}
function test2() {const a = "1"const b = 1console.log(`Number.isInteger(${fmt(a)})`, Number.isInteger(a)) // falseconsole.log(`Number.isInteger(${fmt(b)})`, Number.isInteger(b)) // true}
function test3() {name = 1 // global name will always be a stringconsole.log(fmt(name)) // "1"console.log(`String(${fmt(name)}) === ${fmt(name)}`, String(name) === name) // true}
function test4() {const name = 1 // local nameconsole.log(fmt(name)) // 1console.log(`String(${fmt(name)}) === ${fmt(name)}`, String(name) === name) // false}
test1(); test2(); test3(); test4()
function isFromType(variable, type){if (typeof type == 'string') res = (typeof variable == type.toLowerCase())else res = (variable.constructor == type)return res}
hEllo thErE hEllo thErEIs string? true "hello there"
OH MY OH MYIs string? true "oh my"
368 is a big number 368 is a big numberIs string? true "368"
56839 56839Is string? true "😇"
0 0Is string? true "10"
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>
This shippet only presents functions used in performance tests - it not perform tests itself!
// getTag.js
const toString = Object.prototype.toString;
/*** Gets the `toStringTag` of `value`.** @private* @param {*} value The value to query.* @returns {string} Returns the `toStringTag`.*/function getTag(value) {if (value == null) {return value === undefined? "[object Undefined]": "[object Null]";}return toString.call(value);}
// isString.js
import getTag from "./getTag.js";
/*** Checks if `value` is classified as a `String` primitive or object.** @since 0.1.0* @category Lang* @param {*} value The value to check.* @returns {boolean} Returns `true` if `value` is a string, else `false`.* @example** isString('abc')* // => true** isString(1)* // => false*/function isString(value) {const type = typeof value;return (type === "string" || (type === "object" &&value != null &&!Array.isArray(value) &&getTag(value) == "[object String]"));}
export default isString;