{"id":16293814,"url":"https://github.com/zhaoqize/fe-codebase","last_synced_at":"2026-02-12T08:08:25.734Z","repository":{"id":79007655,"uuid":"129231649","full_name":"zhaoqize/FE-Codebase","owner":"zhaoqize","description":"👣 收集基础且实用的代码片段","archived":false,"fork":false,"pushed_at":"2018-07-26T09:53:29.000Z","size":39,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-29T16:44:40.848Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zhaoqize.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-12T10:04:49.000Z","updated_at":"2018-07-26T09:53:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"ba62e8f3-9dc7-42c7-bf6b-42962d01ad85","html_url":"https://github.com/zhaoqize/FE-Codebase","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/zhaoqize/FE-Codebase","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhaoqize%2FFE-Codebase","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhaoqize%2FFE-Codebase/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhaoqize%2FFE-Codebase/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhaoqize%2FFE-Codebase/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zhaoqize","download_url":"https://codeload.github.com/zhaoqize/FE-Codebase/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhaoqize%2FFE-Codebase/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274929091,"owners_count":25375708,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-13T02:00:10.085Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T20:12:25.755Z","updated_at":"2026-02-12T08:08:20.700Z","avatar_url":"https://github.com/zhaoqize.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# FE-Codebase\n\n### 类型判断\n\n1、是否是Number\n```js\nconst isNumber = val =\u003e {\n  return typeof val === 'number';\n}\n```\n\n2、是否是String\n```js\nconst isString = val =\u003e {\n  return typeof val === 'string';\n}\n```\n\n3、是否是对象\n```js\nconst isObject = obj =\u003e {\n  return obj === Object(obj);\n}\n```\n\n4、是否是undefined\n```js\nconst isUndefined = val =\u003e {\n  return val === undefined;\n};\n```\n\n5、是否是null\n```js\nconst isNull = val =\u003e {\n  return val === null;\n}\n```\n\n6、是否是boolean\n```js\nconst isBoolean = val =\u003e {\n  return typeof val === 'boolean';\n}\n```\n\n7、是否是函数\n```js\nconst isFunction = val =\u003e {\n  return typeof val === 'function';\n}\n```\n\n8、是否是空的\n```js\nconst isEmpty = val =\u003e {\n  return val == null || !(Object.keys(val) || val).length;\n}\n```\n\n### 获取UA\n\n```js\nconst UA = typeof window !== 'undefined' \u0026\u0026 window.navigator.userAgent.toLowerCase();\n```\n\n### 判断是否是IE\n\n```js\nconst isIE = UA \u0026\u0026 /msie|trident/.test(UA)\n```\n\n### 判断是否是IE9\n\n```js\nconst isIE9 = UA \u0026\u0026 UA.indexOf('msie 9.0') \u003e 0;\n```\n\n### 判断是否是Edge\n\n```js\nconst isEdge = UA \u0026\u0026 UA.indexOf('edge/') \u003e 0;\n```\n\n### 判断是否是Android\n\n```js\nconst isAndroid = UA \u0026\u0026 UA.indexOf('android') \u003e 0;\n```\n\n### 判断是否是IOS\n\n```js\nconst isIOS = UA \u0026\u0026 /iphone|ipad|ipod|ios/.test(UA);\n```\n\n### 判断是否是Chrome\n\n```js\nconst isChrome = UA \u0026\u0026 /chrome\\/\\d+/.test(UA) \u0026\u0026 !isEdge;\n```\n\n### 判断是否为Web环境\n\n```js\nconst isBrowser = () =\u003e {\n    return ![typeof window, typeof document].includes('undefined');\n};\n```\n\n### 检测浏览器是否支持Canvas\n\n```js\nconst isSupportCanvas = () {\n    return document.createElement('canvas').getContext ? true : false;\n};\n```\n\n### 常用的正则表达式\n\n```js\nconst isUrl = new RegExp('[a-zA-z]+://[^\\s]*') // 网址判断\nconst isEmail = new RegExp('^\\w+([-+.]\\w+)@\\w+([-.]\\w+).\\w+([-.]\\w+)*$') // 邮箱判断\nconst isChinese = new RegExp('[\\u4e00-\\u9fa5]') // 中文判断\nconst isPhone = new RegExp('^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$') // 中国大陆手机号码\nconst isIDCard = new RegExp('(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)') // 身份证\nconst isPositiveInteger = new RegExp('[0-9]*[1-9][0-9]*') // 正整数\nconst isNegativeInteger = new RegExp('-[0-9]*[1-9][0-9]*') // 负整数\nconst isInteger = new regExp('-?\\d+') // 整数\n```\n\n### URL解析\n\n\u003e 获取查询参数的键值对\n\n```js\nconst getURLParameters = url =\u003e\n  (url.match(/([^?=\u0026]+)(=([^\u0026]*))/g) || []).reduce(\n    (a, v) =\u003e ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a),\n    {}\n  );\n```\n\n### 平滑滚动到页面顶部\n```js\nconst scrollToTop = () =\u003e {\n  const c = document.documentElement.scrollTop || document.body.scrollTop;\n  if (c \u003e 0) {\n    window.requestAnimationFrame(scrollToTop);\n    window.scrollTo(0, c - c / 8);\n  }\n};\n```\n\n### 对象复制\n#### 浅复制\n1、数组方法\n\n```js\narrayObj.slice(0); // 返回一个新数组\n\narrayObj.concat(); // 返回一个新数组\n```\n\n2、for...in\n\n```js\nfunction copy(obj){\n    let newObj = {};\n    for ( let attr in obj) {\n        newObj[attr] = obj[attr];\n    }\n    return newObj;\n}\n```\n\n3、对象解构\n\n\u003e 解构的原理是`Object.assgin`\n\n```js\nlet newObj = {...obj};\n```\n\n4、Object.assgin\n\n```js\nlet newObj = Object.assgin({}, obj);\n```\n\n#### 深复制\n\n1、递归\n\n```js\nfunction deepCopy(obj){\n    if(typeof obj !== 'object'){\n        return obj;\n    }\n    let newObj = {};\n    for ( let item in obj) {\n        newObj[item] = deepCopy(obj[item]);\n    }\n    return newObj;\n}\n```\n\n2、JSON.stringify(黑魔法)\n\n```js\nlet newObj = JSON.parse(JSON.stringify(obj));\n```\n\n### 数组平均数\n\n```js\nconst average = (arr) =\u003e {\n  return arr.reduce((acc, val) =\u003e acc + val, 0) / arr.length;\n}\n```\n\n### 大写每个单词的首字母\n\n```js\nconst capitalizeEveryWord = (str )=\u003e {\n  return str.replace(/\\b[a-z]/g, (char) =\u003e {\n    return char.toUpperCase();\n  });\n}\n```\n\n### 计算数组中某个值的出现次数\n\n```js\nconst countOccurrences = (arr, value) =\u003e {\n  return arr.reduce((a, v) =\u003e {\n    return v === value ? a + 1 : a + 0;\n  }, 0)\n};\n```\n\n### 比较数组之间的区别\n\n```js\nconst difference = (a, b) =\u003e { \n  const s = new Set(b); \n  return a.filter((x) =\u003e {\n    return !s.has(x);\n  }); \n};\n```\n\n### 获取数组中的最大值\n\n```js\nconst arrayMax = (arr) =\u003e {\n  return Math.max(...arr);\n};\n```\n\n### 获取数组中的最小值\n\n```js\nconst arrayMax = (arr) =\u003e {\n  return Math.min(...arr);\n};\n```\n\n### 反转字符串\n\n```js\nconst reverseString = (str) =\u003e {\n  return [...str].reverse().join('');\n};\n```\n\n### 数组之和\n\n```js\nconst sum = (arr) =\u003e {\n  return arr.reduce((acc, val) =\u003e {\n      return acc + val;\n  }, 0);\n}\n```\n\n### 数组转字符串\n\n```js\nconst array2String = function (array) {\n  return array.length === 1 ? array[0] : array.join(' ')\n}\n```\n\n### 获取当前正在执行的脚本\n\n```js\nconst getCurrentScriptSource = funtion () {\n  if (document.currentScript) {\n    return document.currentScript.getAttribute('src');\n  }\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhaoqize%2Ffe-codebase","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzhaoqize%2Ffe-codebase","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhaoqize%2Ffe-codebase/lists"}