String.prototype.strim1 = function () {
return this.replace(/^\s+|\s+$/g, '')
}
String.prototype.strim2 = function () {
return this.replace(/^\s+(.*?)\s+$/, '$1')
}
let str = ' aaaa '
console.log(str.length)
console.log(str)
console.log(str.strim1().length)
console.log(str.strim2().length)
const splitMobile = (mobile, format = '-') => {
return String(mobile).replace(/(?=(\d{4})+$)/g, format)
}
const splitMobile2 = (mobile, format = '-') => {
return String(mobile).replace(/(?<=(\d{3}))/, format).replace(/(?<=([\d\-]{8}))/, format)
}
console.log(splitMobile(18379802267))
console.log(splitMobile2(18379876545))
const _new = function (func, ...args) {
if (typeof func !== 'function') {
throw 'func must be a function'
}
let obj = Object.create(func.prototype)
let result = func.apply(obj, args)
if (typeof result === 'object' && result !== null || typeof result === 'function') {
return result
} else {
return obj
}
}
let Person = function (name, sex) {
this.name = name
this.sex = sex
}
Person.prototype.showInfo = function () {
console.log(this.name, this.sex)
}
let p1 = _new(Person, '前端胖头鱼', 'sex')
console.log(p1)
function fn1(x) {
return x + 1;
}
function fn2(x) {
return x + 2;
}
function fn3(x) {
return x + 3;
}
function fn4(x) {
return x + 4;
}
const a = compose(fn1, fn2, fn3, fn4);
console.log(a(1));
function compose(...fn) {
return fn.reduce((result, it) => {
return (...args) => {
return result(it(...args))
}
}, (it) => it)
}
const deepClone = (target, cache = new Map()) => {
const isObject = (obj) => typeof obj === 'object' && obj !== null
if (isObject(target)) {
const cacheTarget = cache.get(target)
if (cacheTarget) {
return cacheTarget
}
let cloneTarget = Array.isArray(target) ? [] : {}
cache.set(target, cloneTarget)
for (const key in target) {
const value = target[ key ]
cloneTarget[ key ] = isObject(value) ? deepClone(value, cache) : value
}
return cloneTarget
} else {
return target
}
}
const deepClone2 = (target, cache = new Map()) => {
const isObject = (obj) => typeof obj === 'object' && obj !== null
const forEach = (array, cb) => {
const leng = array.length
let i = -1
while (++i < leng) {
cb(array[ i ])
}
}
if (isObject(target)) {
const cacheObj = cache.get(target)
if (cacheObj) {
return cacheObj
}
let cloneTarget = Array.isArray(target) ? [] : {}
let keys = Object.keys(target)
cache.set(target, cloneTarget)
forEach(keys, (key) => {
const value = target[ key ]
cloneTarget[ key ] = isObject(value) ? deepClone2(value, cache) : value
})
return cloneTarget
} else {
return target
}
}
const target = {
field1: 1,
field2: undefined,
field3: {
child: 'child'
},
field4: [2, 4, 8],
f: { f: { f: { f: { f: { f: { f: { f: { f: { f: { f: { f: {} } } } } } } } } } } },
};
target.target = target;
console.time();
const result1 = deepClone(target);
console.log(result1)
console.timeEnd();
console.time();
const result2 = deepClone2(target);
console.log(result2)
console.timeEnd();