CQ linghu
CQ linghu
``` javascript const str = ' s t r ' const POSITION = Object.freeze({ left: Symbol(), right: Symbol(), both: Symbol(), center: Symbol(), all: Symbol(), }) function trim(str, position = POSITION.both)...
考虑一下扩展性 ``` javascript function insertArr(arr, i = 0, min = 2, max = 32) { const num = Math.max(min, Math.ceil(Math.random() * max)) if (!arr[arr.length - 1]) { if (!arr.includes(num)) {...
``` javascript function flat(arr, target) { arr.forEach(item => { if (Array.isArray(item)) { flat(item, target) } else { target.push(item) } }) } function flatArr(arr) { let result = [] flat(arr, result)...
:dog: ``` javascript function delLast(str, target) { return str.split('').reverse().join('').replace(target, '').split('').reverse().join(''); } const str = delLast('asdfghhj', 'h') console.log(str) // asdfghj ```
``` javascript function strCount(str, target) { let count = 0 if (!target) return count while(str.match(target)) { str = str.replace(target, '') count++ } return count } console.log(strCount('abcdef abcdef a', 'abc')) ```
``` javascript function toCamel(str) { return str.replace(/(\w)(_)(\w)/g, (match, $1, $2, $3) => `${$1}${$3.toUpperCase()}`) } console.log(toCamel('a_bc_def')) // aBcDef ```
上面的方法对 a_c_def 会返回 aC_def,想要返回 ACDef 得: ``` javascript function toCamel(str) { str = str.replace(/(\w)/, (match, $1) => `${$1.toUpperCase()}`) while(str.match(/\w_\w/)) { str = str.replace(/(\w)(_)(\w)/, (match, $1, $2, $3) => `${$1}${$3.toUpperCase()}`) }...
``` javascript function caseConvert(str) { return str.split('').map(s => { const code = s.charCodeAt(); if (code < 65 || code > 122 || code > 90 && code < 97) return...
``` javascript function strEncrypt(str) { return str.split('').map(s => { return String.fromCharCode(s.charCodeAt() + 1) }).join('') } console.log(strEncrypt('hello world')) // ifmmp!xpsme ```