{"id":21335197,"url":"https://github.com/jowayyoung/trample","last_synced_at":"2025-04-13T01:11:37.342Z","repository":{"id":57378932,"uuid":"140575365","full_name":"JowayYoung/trample","owner":"JowayYoung","description":"A Web/Node General Utils Library","archived":false,"fork":false,"pushed_at":"2024-09-27T06:40:04.000Z","size":446,"stargazers_count":36,"open_issues_count":2,"forks_count":11,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-10T18:52:35.469Z","etag":null,"topics":["rollup","tool","util"],"latest_commit_sha":null,"homepage":"https://yangzw.vip/source?id=trample","language":"JavaScript","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/JowayYoung.png","metadata":{"files":{"readme":"readme.md","changelog":null,"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-07-11T12:57:34.000Z","updated_at":"2024-06-01T14:51:27.000Z","dependencies_parsed_at":"2025-01-24T11:19:23.390Z","dependency_job_id":null,"html_url":"https://github.com/JowayYoung/trample","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JowayYoung%2Ftrample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JowayYoung%2Ftrample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JowayYoung%2Ftrample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JowayYoung%2Ftrample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JowayYoung","download_url":"https://codeload.github.com/JowayYoung/trample/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650753,"owners_count":21139681,"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","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":["rollup","tool","util"],"created_at":"2024-11-21T23:41:17.280Z","updated_at":"2025-04-13T01:11:37.308Z","avatar_url":"https://github.com/JowayYoung.png","language":"JavaScript","readme":"# Trample \u003cimg src=\"https://img.shields.io/badge/trample-Web/Node通用函数工具库-66f.svg\"\u003e\r\n\r\n[![author](https://img.shields.io/badge/author-JowayYoung-f66.svg)](https://github.com/JowayYoung/trample)\r\n[![version](https://img.shields.io/badge/version-0.4.0-f66.svg)](https://github.com/JowayYoung/trample)\r\n[![web](https://img.shields.io/badge/web-%3E%3D%2095%25-3c9.svg)](https://github.com/JowayYoung/trample)\r\n[![node](https://img.shields.io/badge/node-%3E%3D%208.0.0-3c9.svg)](https://github.com/JowayYoung/trample)\r\n[![test](https://img.shields.io/badge/test-passing-f90.svg)](https://github.com/JowayYoung/trample)\r\n[![build](https://img.shields.io/badge/build-passing-f90.svg)](https://github.com/JowayYoung/trample)\r\n[![coverage](https://img.shields.io/badge/coverage-90%25-09f.svg)](https://github.com/JowayYoung/trample)\r\n[![license](https://img.shields.io/badge/license-MIT-09f.svg)](https://github.com/JowayYoung/trample)\r\n\r\n### 前言\r\n\r\n**工欲善其事，必先利其器**。应用在项目开发上就是：`做好一个项目，前期的工作准备非常重要`。项目开发的前期准备无非都是`合理规划项目结构`、`按需编写构建代码`、`批量创建入口文件`、`复制粘贴工具函数`等。\r\n\r\n在此先推荐笔者写的一个**React/Vue应用自动化构建脚手架**[bruce-cli](https://github.com/JowayYoung/bruce-cli)，其零配置开箱即用的优点非常适合入门级、初中级、快速开发项目的前端同学使用，还可通过创建`brucerc.js`文件来覆盖其默认配置，只需专注业务代码的编写无需关注构建代码的编写，让项目结构更简洁。\r\n\r\n通过`bruce-cli`能把常规的前期准备都解决了，但我们经常会复制粘贴一些之前项目常用的工具函数过来新项目上，新开其他项目时又会重新执行这些操作。\r\n\r\n项目开发过程中时常会重复使用一些工具函数，例如`浏览器类型`、`格式时间差`、`URL参数反序列化`、`过滤XSS`等，为了避免项目开发时重复的复制粘贴操作带来不必要的麻烦，笔者将平时常用的一些工具函数按功能分类和统一封装，并发布到`npm`上。每次项目开发时直接安装，提高开发效率，将时间用在正确的事情上。\r\n\r\n### 安装\r\n\r\n- 使用`npm`安装：`npm i trample`\r\n- 使用`yarn`安装：`yarn add trample`\r\n\r\n### 使用\r\n\r\n`trample`根据**Web**和**Node**两种JS运行环境划分代码，生成两种`bundle.js`。每种文件在不同JS运行环境下运行，必须根据JS运行环境引用文件，否则会报错。\r\n\r\n##### 区别\r\n\r\n模块|工具库|运行环境|对应文件|ESM的对应文件\r\n:-:|:-:|:-:|-|-\r\n**Web**|Web函数工具库|浏览器|`web.js`|`web.esm.js`\r\n**Node**|Node函数工具库|服务器|`node.js`|`node.esm.js`\r\n\r\n##### 兼容\r\n\r\n- **Web**：`\u003e= 95%`\r\n- **Node**：`\u003e= 8.0.0`\r\n\r\n##### 引用\r\n\r\n`trample`使用`rollup`打包，因此可使用`IIFE`、`AMD`、`CJS`、`UMD`和`ESM`五种方式引用。但推荐使用`IIFE`、`CJS`、`ESM`三种引用方式。工具库的代码使用`ESM`规范开发，使用`export {}`导出。\r\n\r\n\u003e IIFE引用方式\r\n\r\n适用于`Web`，最简单最方便的引用方式没有之一。把`node_modules/trample/dist/web.umd.js`复制出来，放到新建的`js/trample`文件夹下，通过HTML的`\u003cscript\u003e`直接引用。\r\n\r\n```html\r\n\u003cbody\u003e\r\n    \u003cscript src=\"js/trample/web.umd.js\"\u003e\u003c/script\u003e\r\n    \u003cscript\u003e\r\n        console.log(window.trample.TypeOf(\"trample\"));\r\n        console.log(window.trample.BrowserType());\r\n    \u003c/script\u003e\r\n\u003c/body\u003e\r\n```\r\n\r\n\u003e AMD引用方式\r\n\r\n适用于`Web`。把`node_modules/trample/dist/web.umd.js`复制出来，放到新建的`js/trample`文件夹下，需建立在`require.js`下使用。\r\n\r\n```js\r\nrequire.config({\r\n    paths: {\r\n        trample: \"js/trample/web.umd.js\"\r\n    }\r\n});\r\nrequire([\"trample\"], function(trample) {\r\n    console.log(trample.BrowserType());\r\n});\r\n```\r\n\r\n\u003e CJS引用方式\r\n\r\n适用于`Web`和`Node`。\r\n\r\n```js\r\n// Web\r\nconst { BrowserType, TypeOf } = require(\"trample\");\r\n// const { BrowserType, TypeOf } = require(\"trample/dist/web\");\r\n\r\nconsole.log(TypeOf(\"trample\"));\r\nconsole.log(BrowserType());\r\n\r\n// Node\r\nconst { NodeType, TypeOf } = require(\"trample/dist/node\");\r\n\r\nconsole.log(TypeOf(\"trample\"));\r\nconsole.log(NodeType());\r\n```\r\n\r\n\u003e ESM引用方式\r\n\r\n适用于`Web`和`Node`。若使用`ESM`规范开发项目，有利于Wepack启用`TreeSharking`移除未使用的代码。\r\n\r\n```js\r\n// Web\r\nimport { BrowserType, TypeOf } from \"trample\";\r\n// import { BrowserType, TypeOf } from \"trample/dist/web\";\r\n\r\nconsole.log(TypeOf(\"trample\"));\r\nconsole.log(BrowserType());\r\n\r\n// Node\r\nimport { NodeType, TypeOf } from \"trample/dist/node\";\r\n\r\nconsole.log(TypeOf(\"trample\"));\r\nconsole.log(NodeType());\r\n```\r\n\r\n##### 再次提醒\r\n\r\n在`Web`环境下，请使用以下导入方式⏬。`trample`默认引用`node_modules/trample/dist/web.js`这个文件。\r\n\r\n```js\r\nconst { BrowserType, TypeOf } = require(\"trample\");\r\n// 或\r\nimport { BrowserType, TypeOf } from \"trample\";\r\n\r\n// 『上面两段代码』 等价 『下面两段代码』(推荐)\r\n\r\nconst { BrowserType, TypeOf } = require(\"trample/dist/web\");\r\n// 或\r\nimport { BrowserType, TypeOf } from \"trample/dist/web\";\r\n```\r\n\r\n在`Node`环境下，请使用以下导入方式⏬。\r\n\r\n```js\r\nconst { NodeType, TypeOf } = require(\"trample/dist/node\");\r\n// 或\r\nimport { NodeType, TypeOf } from \"trample/dist/node\";\r\n```\r\n\r\n`trample`提供`ESM`规范的`index.esm.js`，在`package.json`中已配置`module`字段指向`index.esm.js`。\r\n\r\n若使用`webpack`打包项目可利用`Tree Sharking`特性移除未使用代码，有效减小打包体积。配置如下。\r\n\r\n```js\r\nmodule.exports = {\r\n    // webpack其他配置\r\n    resolve: {\r\n        mainFields: [\"module\", \"jsnext:main\", \"main\"]\r\n    }\r\n};\r\n```\r\n\r\n### 文档\r\n\r\n```!\r\n暂时未接入TypeScript实现函数入参校验，请遵循文档指定的入参类型传参\r\n```\r\n\r\n##### 公共函数工具库\r\n\r\n[Array 数组工具](https://github.com/JowayYoung/trample/blob/master/src/common/array.js)\r\n\r\n- [x] **GroupMemKey()**：分组成员特性\r\n\t- arr：数组(`[]`)\r\n\t- key：属性(`\"\"`)\r\n- [x] **RecordMemPosition()**：记录成员位置\r\n\t- arr：数组(`[]`)\r\n\t- val：值(`\"\"`)\r\n- [x] **StatMemCount()**：统计成员个数\r\n\t- arr：数组(`[]`)\r\n- [x] **StatMemKeyword()**：统计成员所含关键字\r\n\t- arr：数组(`[]`)\r\n\t- keys：关键字集合(`[]`)\r\n\r\n```js\r\nconst arr = [\r\n    { area: \"GZ\", name: \"YZW\", age: 27 },\r\n    { area: \"GZ\", name: \"TYJ\", age: 25 },\r\n    { area: \"GZ\", name: \"LJY\", age: 26 },\r\n    { area: \"FS\", name: \"LXY\", age: 24 }\r\n];\r\nGroupMemKey(arr, \"area\"); // { GZ: Array(3), FS: Array(1) }\r\n\r\nconst arr = [2, 1, 5, 4, 2, 1, 6, 6, 7];\r\nRecordMemPosition(arr, 2); // [0, 4]\r\n\r\nconst arr = [0, 1, 1, 2, 2, 2];\r\nStatMemCount(arr); // { 0: 1, 1: 2, 2: 3 }\r\n\r\nconst text = [\r\n    \"今天天气真好，我想出去钓鱼\",\r\n    \"我一边看电视，一边写作业\",\r\n    \"小明喜欢同桌的小红，又喜欢后桌的小君，真TM花心\",\r\n    \"最近上班喜欢摸鱼的人实在太多了，代码不好好写，在想入非非\"\r\n];\r\nconst keyword = [\"偷懒\", \"喜欢\", \"睡觉\", \"摸鱼\", \"真好\", \"一边\", \"明天\"];\r\nStatMemKeyword(text, keyword); // [\"喜欢\", \"摸鱼\", \"真好\", \"一边\"]\r\n```\r\n\r\n[Date 日期工具](https://github.com/JowayYoung/trample/blob/master/src/common/date.js)\r\n\r\n- [x] **FormatCountdown()**：格式倒计时\r\n\t- time：时间(`null`，格式为`YYYY-MM-DD HH:mm:ss`，Safari格式为`YYYY/MM/DD HH:mm:ss`)\r\n\t- 备注：用于`未来时间`\r\n- [x] **FormatDiffTime()**：格式时间差\r\n\t- time：时间(`null`，格式为`YYYY-MM-DD HH:mm:ss`，Safari格式为`YYYY/MM/DD HH:mm:ss`)\r\n\t- 备注：可用于`未来时间`或`过去时间`\r\n\r\n```js\r\nFormatCountdown(\"2021-01-31\"); // \"367天15时55分17秒\"\r\n\r\nFormatDiffTime(\"2019-03-31\"); // \"10个月前\"\r\n```\r\n\r\n[Function 函数工具](https://github.com/JowayYoung/trample/blob/master/src/common/function.js)\r\n\r\n- [x] **AsyncTo()**：格式异步返回值\r\n\t- pfn：Promise函数(`Promise.resolve(true)`)\r\n\t- 备注：必须在`async函数`或`自执行async函数`下使用\r\n- [x] **Debounce()**：防抖\r\n\t- fn：函数(`v =\u003e v`)\r\n\t- dura：时延(`50`)\r\n- [x] **Throttle()**：节流\r\n\t- fn：函数(`v =\u003e v`)\r\n\t- dura：时延(`50`)\r\n- [x] **WaitFor()**：等待\r\n\t- dura：时延(`1000`)\r\n\t- 备注：必须在`async函数`或`自执行async函数`下使用\r\n\r\n```js\r\ndocument.body.addEventListener(\"click\", () =\u003e Debounce(() =\u003e console.log(\"Click\"), 2000));\r\n\r\ndocument.body.addEventListener(\"scroll\", () =\u003e Throttle(() =\u003e console.log(\"Scroll\"), 2000));\r\n\r\n(async() =\u003e {\r\n    const [err, res] = await AsyncTo(GetData());\r\n    await WaitFor(2000);\r\n    console.log(err, res);\r\n})();\r\n```\r\n\r\n[Number 数值工具](https://github.com/JowayYoung/trample/blob/master/src/common/number.js)\r\n\r\n- [x] **ByteSize()**：字节大小\r\n\t- byte：字节(`0`)\r\n- [x] **FillNum()**：补零数值\r\n\t- num：数值(`0`)\r\n\t- len：补位(`0`)\r\n- [x] **RandomNum()**：范围随机数\r\n\t- min：最小数(`0`)\r\n\t- max：最大数(`10`)\r\n- [x] **RandomNumPlus()**：N个范围随机数\r\n\t- min：最小数(`0`)\r\n\t- max：最大数(`10`)\r\n\t- count：个数(`1`)\r\n- [x] **RoundNum()**：精确数值(`四舍五入`和`百分比`)\r\n\t- num：数值(`0`)\r\n\t- dec：小数个数(`2`)\r\n\t- per：是否百分比(`false`)\r\n- [x] **ThousandNum()**：千分数值\r\n\t- num：数值(`0`)\r\n\r\n```js\r\nByteSize(683468); // \"667 KB\"\r\n\r\nFillNum(999, 4); // \"0999\"\r\n\r\nRandomNum(0, 100); // 88\r\n\r\nRandomNumPlus(0, 100, 3); // [40, 59, 27]\r\n\r\nRoundNum(0.331234, 2, true); // \"33.12%\"\r\n\r\nThousandNum(12345.6789); // \"12,345.6,789\"\r\n```\r\n\r\n[Object 对象工具](https://github.com/JowayYoung/trample/blob/master/src/common/object.js)\r\n\r\n- [x] **GetKeys()**：读取属性\r\n\t- obj：对象(`{}`)\r\n\t- keys：属性集合(`[]`)\r\n\r\n```js\r\nconst obj = { a: 1, b: 2, c: 3, d: 4 };\r\nconst keys = [\"a\", \"d\"];\r\nGetKeys(obj, keys); // { a: 1, d: 4 }\r\n```\r\n\r\n[Regexp 正则工具](https://github.com/JowayYoung/trample/blob/master/src/common/regexp.js)\r\n\r\n- [x] **CheckText()**：校验文本\r\n\t- type：类型(`\"\"`，可选`address地址、count数量、date日期、email邮件、idcard身份证、image图片、name名称、number计数、password密码、phone手机`)\r\n\t- text：文本(`\"\"`)\r\n\t- 备注：内置以上几种常用校验文本，如不符合需求请使用以下的`CheckTextPlus()`\r\n- [x] **CheckTextPlus()**：自定义校验文本\r\n\t- regexp：正则(`new RegExp()`)\r\n\t- msg：提示(`\"\"`)\r\n\t- text：文本(`\"\"`)\r\n- [x] **MatchBracketText()**：匹配括号文本\r\n\t- tgt：括号形式(`\"(*)\"`，提取的内容必须使用`*`代替)\r\n\t- text：文本(`\"\"`)\r\n\r\n```js\r\nCheckText(\"email\", \"young.joway@aliyun\"); // { flag: false, msg: \"邮箱只能由xxx@yyy.zzz形式组成\" }\r\n\r\nCheckTextPlus(/^(fe)?male$/g, \"性别输入错误\", \"male\"); // { flag: true, msg: \"\" }\r\n\r\nMatchBracketText(\r\n    \"\u003cimg src=\\\"*\\\"\u003e\",\r\n    \"\u003cimg src=\\\"pig.jpg\\\"\u003e\u003cp\u003etrample\u003c/p\u003e\u003cimg src=\\\"dada.png\\\"\u003e\"\r\n); // [\"pig.jpg\", \"dada.png\"]\r\n```\r\n\r\n[String 字符工具](https://github.com/JowayYoung/trample/blob/master/src/common/string.js)\r\n\r\n- [x] **DesePhone()**：脱敏手机\r\n\t- phone：手机(`\"\"`)\r\n- [x] **FormatPhone()**：格式手机\r\n\t- phone：手机(`\"\"`)\r\n\t- sign：标记(`\"-\"`，可选`-、\\s`)\r\n- [x] **RandomColor()**：随机HEX色值\r\n- [x] **RandomId()**：随机长度ID\r\n\t- len：长度(`5`，在`1~10`间)\r\n- [x] **RemoveTag()**：移除标签\r\n\t- text：文本(`\"\"`)\r\n- [x] **ReverseText()**：翻转文本\r\n\t- text：文本(`\"\"`)\r\n- [x] **StartScore()**：星级评分\r\n\t- rate：星级(`0`，在`0~len`间)\r\n\t- len：长度(`5`)\r\n\r\n```js\r\nDesePhone(\"18866669999\"); // \"188****9999\"\r\n\r\nFormatPhone(\"18866669999\", \" \"); // \"188 6666 9999\"\r\n\r\nRandomColor(); // \"#26f455\"\r\n\r\nRandomId(8); // \"6ohsln3s\"\r\n\r\nRemoveTag(\"\u003cscript\u003ealert(\\\"hello world\\\")\u003c/script\u003e\"); // \"alert(\"hello world\")\"\r\n\r\nReverseText(\"trample\"); // \"elpmart\"\r\n\r\nStartScore(8, 10); // \"★★★★★★★★☆☆\"\r\n```\r\n\r\n[Type 类型工具](https://github.com/JowayYoung/trample/blob/master/src/common/type.js)\r\n\r\n- [x] **CompareObj()**：比较对象(`包含数组`)\r\n\t- obj1：对象1\r\n\t- obj2：对象2\r\n- [x] **EnvType()**：环境类型\r\n\t- **IsNode()**：判断Node\r\n\t- **IsWeb()**：判断Web\r\n- [x] **IsEqual()**：判断相等\r\n\t- data1：数据1\r\n\t- data2：数据2\r\n- [x] **TypeOf()**：数据类型\r\n\t- data：数据\r\n\t- type：类型\r\n\t- **IsArguments()**：判断Arguments\r\n\t- **IsArray()**：判断数组\r\n\t- **IsAsyncFunction()**：判断异步函数\r\n\t- **IsBoolean()**：判断布尔值\r\n\t- **IsClass()**：判断类\r\n\t- **IsDate()**：判断日期\r\n\t- **IsEmpty()**：判断空\r\n\t- **IsEmptyArray()**：判断空数组\r\n\t- **IsEmptyObject()**：判断空对象\r\n\t- **IsError()**：判断错误\r\n\t- **IsFunction()**：判断函数\r\n\t- **IsMap()**：判断Map\r\n\t- **IsNull()**：判断空值\r\n\t- **IsNumber()**：判断数值\r\n\t- **IsObject()**：判断对象\r\n\t- **IsRegExp()**：判断正则\r\n\t- **IsSet()**：判断Set\r\n\t- **IsString()**：判断字符串\r\n\t- **IsSymbol()**：判断Symbol\r\n\t- **IsSyncFunction()**：判断同步函数\r\n\t- **IsUndefined()**：判断未定义\r\n\t- **IsWeakMap()**：判断WeakMap\r\n\t- **IsWeakSet()**：判断WeakSet\r\n\r\n```js\r\nCompareObj({ a: 1, b: 2 }, { b: 3, a: 1 }); // { a: true, b: false }\r\n\r\nEnvType(); // \"node\"\r\nIsNode(); // true\r\nIsWeb(); // false\r\n\r\nIsEqual({ a: 1, b: 2 }, { b: 2, a: 1 }); // true\r\n\r\nTypeOf(168); // \"number\"\r\nIsEmptyObject({ a: 1, b: 2 }); // false\r\nIsString(168); // false\r\n```\r\n\r\n##### Web函数工具库\r\n\r\n[Cookie Cookie工具](https://github.com/JowayYoung/trample/blob/master/src/web/cookie.js)\r\n\r\n- [x] **GetCookie()**：读取Cookie\r\n- [x] **RemoveCookie()**：删除Cookie\r\n\t- key：键(`\"\"`)\r\n- [x] **SetCookie()**：设置Cookie\r\n\t- key：键(`\"\"`)\r\n\t- val：值(`\"\"`)\r\n\t- day：过期时间(`1`，日)\r\n\r\n```js\r\nGetCookie(); // { user_id: \"12345\", user_token: \"abcde\" }\r\n\r\nRemoveCookie(\"user_id\");\r\n\r\nSetCookie(\"user_id\", \"123abc\", 7);\r\n```\r\n\r\n[DOM DOM工具](https://github.com/JowayYoung/trample/blob/master/src/web/dom.js)\r\n\r\n- [x] **AutoResponse()**：自适应\r\n\t- width：设计图宽度(`750`)\r\n\t- 备注：在DOM加载前使用，使DOM的尺寸自适应且使用`rem`定义(`1rem=100px`)，例如设计图里的按钮长宽是`100px * 40px`，则在CSS上书写`.btn{width:1rem;height:.4rem;}`\r\n- [x] **CopyPaste()**：复制粘贴\r\n\t- elem：节点(`document.body`)\r\n- [x] **DownloadFile()**：下载文件\r\n\t- url：地址(`\"\"`)\r\n\t- name：文件名(`\"\"`)\r\n- [x] **FilterXss()**：过滤XSS\r\n\t- html：HTML内容(`\"\"`)\r\n- [x] **Img2Base64()**：图像转B64\r\n\t- url：地址(`\"\"`)\r\n\t- type：类型(`\"image/png\"`，可选`image/jpeg、image/png`)\r\n- [x] **Jsonp()**：JSONP\r\n\t- url：地址(`\"\"`)\r\n\t- name：全局变量(`\"jsonp\"`)\r\n\t- cb：回调函数(`null`)\r\n- [x] **LoadScript()**：加载脚本\r\n\t- url：地址(`\"\"`)\r\n\t- pst：插入位置(`\"body\"`，可选`head、body`)\r\n\r\n```js\r\nAutoResponse(640);\r\n\r\nCopyPaste(document.getElementById(\"btn\"));\r\n\r\nDownloadFile(\"https://xxx.yyy/pig.jpg\", \"pig.jpg\");\r\n\r\nFilterXss(\"\u003cscript\u003ealert(123)\u003c/script\u003e\"); // \"\u0026lt;script\u0026gt;alert(123)\u0026lt;/script\u0026gt;\"\r\n\r\nconst img = await Img2Base64(\"https://xxx.yyy/pig.jpg\", \"image/jpeg\"); // \"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQAB...\"\r\n\r\nconst flag = await Jsonp(\"https://xxx.yyy/trample.js\", \"trample\", () =\u003e console.log(window.trample));\r\n\r\nconst flag = await LoadScript(\"https://xxx.yyy/trample.js\", \"body\");\r\n```\r\n\r\n[Function 函数工具](https://github.com/JowayYoung/trample/blob/master/src/web/function.js)\r\n\r\n- [x] **Ajax({ ... })**：异步请求\r\n\t- data：参数集合(`{}`)\r\n\t- error：失败回调函数(`null`)\r\n\t- success：成功回调函数(`null`)\r\n\t- type：类型(`\"get\"`，可选`get、post`)\r\n\t- url: 地址(`\"\"`)\r\n\r\n```js\r\nAjax({\r\n    data: { a: 1, b: 2 },\r\n    error: err =\u003e console.log(err),\r\n    success: res =\u003e console.log(res),\r\n    type: \"post\",\r\n    url: \"https://xxx.yyy\"\r\n});\r\n```\r\n\r\n[Storage Storage工具](https://github.com/JowayYoung/trample/blob/master/src/web/storage.js)\r\n\r\n- [x] **ClearLStorage**：清空LocalStorage\r\n- [x] **ClearSStorage**：清空SessionStorage\r\n- [x] **GetLStorage**：读取LocalStorage\r\n\t- key：键(`\"\"`)\r\n- [x] **GetSStorage**：读取SessionStorage\r\n\t- key：键(`\"\"`)\r\n- [x] **RemoveLStorage**：移除LocalStorage\r\n\t- key：键(`\"\"`)\r\n- [x] **RemoveSStorage**：移除SessionStorage\r\n\t- key：键(`\"\"`)\r\n- [x] **SetLStorage**：设置LocalStorage\r\n\t- key：键(`\"\"`)\r\n\t- val：值(`\"\"`)\r\n- [x] **SetSStorage**：设置SessionStorage\r\n\t- key：键(`\"\"`)\r\n\t- val：值(`\"\"`)\r\n\r\n```js\r\nClearLStorage();\r\nClearSStorage();\r\n\r\nGetLStorage(\"love\"); // \"Love\"\r\nGetSStorage(\"love\"); // \"Love\"\r\n\r\nRemoveSStorage(\"love\");\r\nRemoveSStorage(\"love\");\r\n\r\nSetLStorage(\"love\", \"我爱你\");\r\nSetSStorage(\"love\", \"我爱你\");\r\n```\r\n\r\n[Type 类型工具](https://github.com/JowayYoung/trample/blob/master/src/web/type.js)\r\n\r\n- [x] **BrowserType()**：浏览器类型(史上最全的浏览器类型判断，详情请戳[《详细判断浏览器运行环境》](https://juejin.im/post/5d0220a8f265da1bcc193c6c))\r\n\t- ua：用户代理(`navigator.userAgent.toLowerCase()`)\r\n- [x] **IsElement()**：判断Element\r\n\t- data：数据\r\n\r\n```js\r\nBrowserType(); // { engine: \"webkit\", engineVs: \"537.36\", platform: \"desktop\", supporter: \"chrome\", supporterVs: \"78.0.3904.108\", system: \"macos\", systemVs: \"10.14.6\" }\r\n\r\nIsElement(document.body); // true\r\n```\r\n\r\n[URL URL工具](https://github.com/JowayYoung/trample/blob/master/src/web/url.js)\r\n\r\n- [x] **ParseUrlSearch()**：URL参数反序列化\r\n- [x] **RemoveUrlSearch()**：删除URL参数\r\n\t- search：参数集合(`...[]`，多参数输入)\r\n- [x] **SetUrlSearch()**：设置URL参数\r\n\t- search：参数集合(`{}`)\r\n- [x] **StringifyUrlSearch()**：URL参数序列化\r\n\t- search：参数集合(`{}`)\r\n\t- clear：是否清除假值(`false`，假值包含`undefined、null、\"\"、NaN`)\r\n\r\n```js\r\nParseUrlSearch(); // { name: \"young\", sex: \"male\" }\r\n\r\nRemoveUrlSearch(\"name\", \"sex\");\r\n\r\nSetUrlSearch({ name: \"tong\", sex: \"female\" });\r\n\r\nStringifyUrlSearch({ address: \"\", name: \"young\", sex: \"male\" }, true); // \"?name=young\u0026sex=male\"\r\n```\r\n\r\n##### Node函数工具库\r\n\r\n[Fs 文件工具](https://github.com/JowayYoung/trample/blob/master/src/node/fs.js)\r\n\r\n- [x] **CopyDir()**：复制文件路径\r\n\t- src：输入路径(`\"\"`)\r\n\t- dist: 输出路径(`\"\"`)\r\n\t- filter：过滤函数(`false`，返回`函数`表示过滤规则，返回`false`表示不复制)，函数入参为`stat`和`path`\r\n- [x] **CreateDir()**：创建文件路径\r\n\t- dir：路径(`\"\"`)\r\n- [x] **ReadFileForBFS**：BFS读取文件(`广度优先遍历`)\r\n\t- dir：路径(`process.cwd()`)\r\n\t- igonre：忽略文件正则(`/(node_modules|\\.git|\\.DS_Store)$/`)\r\n- [x] **ReadFileForDFS**：DFS读取文件(`深度优先遍历`)\r\n\t- dir：路径(`process.cwd()`)\r\n\t- igonre：忽略文件正则(`/(node_modules|\\.git|\\.DS_Store)$/`)\r\n- [x] **RemoveDir()**：删除文件路径\r\n\t- dir：路径(`\"\"`)\r\n\r\n```js\r\nimport Path from \"path\";\r\n\r\nfunction AbsPath(dir) {\r\n    return Path.join(__dirname, dir);\r\n}\r\n\r\nCopyDir(\r\n    AbsPath(\"./src\"),\r\n    AbsPath(\"./trample\"),\r\n    (stat, path) =\u003e !(stat === \"file\" \u0026\u0026 path.includes(\".DS_Store\"))\r\n);\r\n\r\nCreateDir(AbsPath(\"./assets/lib/trample\"));\r\n\r\nReadFileForBFS(); // [\"node.js\", \"web.js\"]\r\n\r\nReadFileForDFS(); // [\"node.js\", \"web.js\"]\r\n\r\nRemoveDir(AbsPath(\"./assets/lib/trample\"));\r\n```\r\n\r\n[Process 进程工具](https://github.com/JowayYoung/trample/blob/master/src/node/process.js)\r\n\r\n- [x] **RunCmd()**：运行命令\r\n\t- cmd：命令行(`\"node -v\"`)\r\n\t- 备注：只支持单行命令，多个命令同时执行可书写成`cmd1 \u0026\u0026 cmd2`\r\n\r\n```js\r\nRunCmd(\"npm -v\"); // \"6.11.3\"\r\n```\r\n\r\n[Type 类型工具](https://github.com/JowayYoung/trample/blob/master/src/node/type.js)\r\n\r\n- [x] **NodeType()**：Node类型\r\n\r\n```js\r\nNodeType(); // { nodeVs: \"12.12.0\", npmVs: \"6.11.3\", system: \"macos\", systemVs: \"18.7.0\" }\r\n```\r\n\r\n### 目录结构\r\n\r\n\u003e 开发目录\r\n\r\n```txt\r\ntrample\r\n├─ src\r\n│  ├─ common\r\n│  │  ├─ array.js\r\n│  │  ├─ date.js\r\n│  │  ├─ function.js\r\n│  │  ├─ index.js\r\n│  │  ├─ number.js\r\n│  │  ├─ object.js\r\n│  │  ├─ regexp.js\r\n│  │  ├─ string.js\r\n│  │  └─ type.js\r\n│  ├─ node\r\n│  │  ├─ fs.js\r\n│  │  ├─ index.js\r\n│  │  ├─ process.js\r\n│  │  └─ type.js\r\n│  ├─ web\r\n│  │  ├─ cookie.js\r\n│  │  ├─ dom.js\r\n│  │  ├─ function.js\r\n│  │  ├─ index.js\r\n│  │  ├─ storage.js\r\n│  │  ├─ type.js\r\n│  │  └─ url.js\r\n│  ├─ node.js\r\n│  ├─ web.js\r\n│  └─ web.umd.js\r\n├─ .gitignore\r\n├─ .npmignore\r\n├─ license\r\n├─ package.json\r\n├─ readme.md\r\n└─ rollup.config.js\r\n```\r\n\r\n### 构建编译\r\n\r\n后续补发，构建编译过程正在完善中......\r\n\r\n### 测试用例\r\n\r\n后续补发，测试用例流程正在书写中......\r\n\r\n### 待做\r\n\r\n- [ ] 补全测试用例\r\n- [ ] 接入TypeScript\r\n\r\n### 后记\r\n\r\n`trample`是笔者为了节省项目开发过程中常用工具函数复制粘贴的时间，而封装的一个**Web/Node通用函数工具库**。设计目的是为了`减少无谓的复制粘贴动作`和`统一管理项目开发中常用的工具函数`。\r\n\r\n由于笔者是针对个人需求而定制的工具库，所以应用范围可能未包含上你常用的工具函数，可在[Issue](https://github.com/JowayYoung/trample/issues)上`提出你的宝贵建议`或`贴上你想增加的工具函数`。笔者会认真阅读你的宝贵建议和整合各位同学贡献的工具函数。\r\n\r\n也可[Fork](https://github.com/JowayYoung/trample)本项目到自己的`Github`上，在原有的基础上增加自己`常用`、`易忘`和`代码量多`的工具函数，同时也可扩展原有的功能和构建方式，封装成自己熟悉的工具库，提升自己的开发能力，间接`减少晚上加班时间`和`增加上班摸鱼时间`。\r\n\r\n若觉得`trample`对你有帮助，可在[Issue](https://github.com/JowayYoung/trample/issues)上`提出你的宝贵建议`，笔者会认真阅读并整合你的建议。喜欢`trample`的请给一个[Star](https://github.com/JowayYoung/trample)，或[Fork](https://github.com/JowayYoung/trample)本项目到自己的`Github`上，根据自身需求定制功能。\r\n\r\n**关注公众号`IQ前端`，一个专注于CSS/JS开发技巧的前端公众号，更多前端小干货等着你喔**\r\n\r\n![](https://static.yangzw.vip/frontend/account/IQ前端公众号.jpg)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjowayyoung%2Ftrample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjowayyoung%2Ftrample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjowayyoung%2Ftrample/lists"}