├── .eslintrc ├── .github └── workflows │ ├── nodejs-14.yml │ ├── nodejs.yml │ ├── pkg.pr.new.yml │ └── release.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE.txt ├── README.md ├── benchmark ├── YYYYMMDDHHmmss.cjs ├── arguments_to_array.cjs ├── array_splice.cjs ├── assign-array.cjs ├── assign.cjs ├── date_YYYYMMDD.cjs ├── date_format.cjs ├── encodeURIComponent.cjs ├── get_paramnames.cjs ├── map.cjs ├── md5.cjs ├── random_integer.cjs ├── random_string.cjs ├── replaceInvalidHttpHeaderChar.cjs ├── sha512.cjs ├── split.cjs └── string_tpl.cjs ├── package.json ├── src ├── array.ts ├── crypto.ts ├── date.ts ├── fs.ts ├── function.ts ├── index.ts ├── json.ts ├── number.ts ├── object.ts ├── optimize.ts ├── string.ts ├── timeout.ts └── web.ts ├── test ├── array.test.ts ├── crypto.test.ts ├── date.test.ts ├── fs.test.ts ├── function.test.ts ├── json.test.ts ├── number.test.ts ├── object.test.ts ├── optimize.test.ts ├── string.test.ts ├── timeout.test.ts ├── tmp │ ├── target │ ├── target1 │ └── target2 └── web.test.ts └── tsconfig.json /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "eslint-config-egg/typescript", 4 | "eslint-config-egg/lib/rules/enforce-node-prefix" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /.github/workflows/nodejs-14.yml: -------------------------------------------------------------------------------- 1 | name: Node.js 14 CI 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v4 15 | - name: Use Node.js 16 | uses: irby/setup-node-nvm@master 17 | with: 18 | node-version: '16.x' 19 | - run: npm install 20 | - run: npm run prepublishOnly 21 | - run: node -v 22 | - run: . /home/runner/mynvm/nvm.sh && nvm install 14 && nvm use 14 && node -v && npm run test-local 23 | -------------------------------------------------------------------------------- /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | Job: 11 | name: Node.js 12 | uses: node-modules/github-actions/.github/workflows/node-test.yml@master 13 | with: 14 | version: '16, 18, 20, 22, 23' 15 | secrets: 16 | CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} 17 | -------------------------------------------------------------------------------- /.github/workflows/pkg.pr.new.yml: -------------------------------------------------------------------------------- 1 | name: Publish Any Commit 2 | on: [push, pull_request] 3 | 4 | jobs: 5 | build: 6 | runs-on: ubuntu-latest 7 | 8 | steps: 9 | - name: Checkout code 10 | uses: actions/checkout@v4 11 | 12 | - run: corepack enable 13 | - uses: actions/setup-node@v4 14 | with: 15 | node-version: 20 16 | 17 | - name: Install dependencies 18 | run: npm install 19 | 20 | - name: Build 21 | run: npm run prepublishOnly --if-present 22 | 23 | - run: npx pkg-pr-new publish 24 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | 7 | jobs: 8 | release: 9 | name: Node.js 10 | uses: node-modules/github-actions/.github/workflows/node-release.yml@master 11 | secrets: 12 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 13 | GIT_TOKEN: ${{ secrets.GIT_TOKEN }} 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | coverage 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | node_modules 15 | npm-debug.log 16 | .DS_Store 17 | .idea 18 | .nyc_output 19 | .tshy* 20 | dist 21 | package-lock.json 22 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [2.5.0](https://github.com/node-modules/utility/compare/v2.4.0...v2.5.0) (2025-01-14) 4 | 5 | 6 | ### Features 7 | 8 | * support convert milliseconds and fix timezone ([#66](https://github.com/node-modules/utility/issues/66)) ([4bc6691](https://github.com/node-modules/utility/commit/4bc669117456c1ad8936cd1f1ce46489d4f6153f)) 9 | 10 | ## [2.4.0](https://github.com/node-modules/utility/compare/v2.3.0...v2.4.0) (2024-12-22) 11 | 12 | 13 | ### Features 14 | 15 | * support fs.exists async function ([#65](https://github.com/node-modules/utility/issues/65)) ([eb95f36](https://github.com/node-modules/utility/commit/eb95f36dea2a7cebb32d46038cc99b455fb734cd)) 16 | 17 | ## [2.3.0](https://github.com/node-modules/utility/compare/v2.2.0...v2.3.0) (2024-12-18) 18 | 19 | 20 | ### Features 21 | 22 | * support promise with timeout ([#64](https://github.com/node-modules/utility/issues/64)) ([bc3e168](https://github.com/node-modules/utility/commit/bc3e168b0f38180053481f95a5cc653b775b2d79)) 23 | 24 | ## [2.2.0](https://github.com/node-modules/utility/compare/v2.1.0...v2.2.0) (2024-12-10) 25 | 26 | 27 | ### Features 28 | 29 | * export getDateStringParts ([#63](https://github.com/node-modules/utility/issues/63)) ([3475532](https://github.com/node-modules/utility/commit/3475532e8e158967211a7aefaccdf71ea9bf4d88)) 30 | 31 | ## [2.1.0](https://github.com/node-modules/utility/compare/v2.0.0...v2.1.0) (2024-03-13) 32 | 33 | 34 | ### Features 35 | 36 | * try to use crypto.hash first ([#62](https://github.com/node-modules/utility/issues/62)) ([2a68056](https://github.com/node-modules/utility/commit/2a68056f7c39be6c9ec1cdc954f42a45e8a68dc3)) 37 | 38 | ## [2.0.0](https://github.com/node-modules/utility/compare/v1.18.0...v2.0.0) (2024-01-13) 39 | 40 | 41 | ### ⚠ BREAKING CHANGES 42 | 43 | * drop Node.js < 16 support 44 | 45 | ### Features 46 | 47 | * refactor using typescript ([#60](https://github.com/node-modules/utility/issues/60)) ([e20c1d9](https://github.com/node-modules/utility/commit/e20c1d9702912f5ebc6f3c038bf98338176d4f53)) 48 | 49 | ## [1.18.0](https://github.com/node-modules/utility/compare/v1.17.0...v1.18.0) (2023-04-26) 50 | 51 | 52 | ### Features 53 | 54 | * require speedup ([#59](https://github.com/node-modules/utility/issues/59)) ([0503e39](https://github.com/node-modules/utility/commit/0503e390a3af3ab1a785f582107fd0a0c2e75366)) 55 | 56 | --- 57 | 58 | 59 | 1.17.0 / 2020-12-15 60 | ================== 61 | 62 | **features** 63 | * [[`19c96a8`](http://github.com/node-modules/utility/commit/19c96a8a58e6bbb4c8bf572b9d2fb76372f16e1e)] - feat: hash ts define support object params (#55) (恒遥 <>) 64 | 65 | **others** 66 | * [[`181f2ed`](http://github.com/node-modules/utility/commit/181f2ed90586258c3110ac7c149f66b8bc7b82b8)] - test: drop node 4, 6 on travis ci (#56) (fengmk2 <>) 67 | 68 | 1.16.3 / 2019-11-20 69 | ================== 70 | 71 | **fixes** 72 | * [[`cf79437`](http://github.com/node-modules/utility/commit/cf794378d9a567b2acdbe894fb0fe86bb11c8e31)] - fix: do not use ECMAScript6 syntax. (#52) (ULIVZ <<472590061@qq.com>>) 73 | 74 | 1.16.2 / 2019-11-19 75 | ================== 76 | 77 | **fixes** 78 | * [[`bb8f5d4`](http://github.com/node-modules/utility/commit/bb8f5d4becb61df6e8ce1788a172f8ae4e9937b4)] - fix: getParamNames throw when input is not function (#51) (Daniels.Sun <>) 79 | 80 | **others** 81 | * [[`7016803`](http://github.com/node-modules/utility/commit/7016803364f22c70c42da59870eee64dd1d99020)] - build: add node 12 in ci (dead-horse <>) 82 | 83 | 1.16.1 / 2019-03-25 84 | ================== 85 | 86 | **fixes** 87 | * [[`a8b0c0d`](http://github.com/node-modules/utility/commit/a8b0c0d38e7d2f6c85169975cfc7f2f1a033c310)] - fix: detect number first before check NaN (#48) (fengmk2 <>) 88 | 89 | 1.16.0 / 2019-03-25 90 | ================== 91 | 92 | **features** 93 | * [[`16104be`](http://github.com/node-modules/utility/commit/16104befbfde9c2a0bf95b88029436c4a182b1bb)] - feat: add getOwnEnumerables(obj, ignoreNull) (#47) (fengmk2 <>) 94 | 95 | 1.15.1 / 2019-03-13 96 | ================== 97 | 98 | **features** 99 | * [[`b092817`](http://github.com/node-modules/utility/commit/b092817be0c4de459bde180e754ba4872e715027)] - feat: support for ts (#45) (yyge <<33921398+ddzy@users.noreply.github.com>>) 100 | 101 | **others** 102 | * [[`09bd9d0`](http://github.com/node-modules/utility/commit/09bd9d061b57a2ef6b24a39b41063f51cf251fc1)] - chore: package.json files add [index.d.ts] (#46) (ccccQ <>) 103 | * [[`7c102f4`](http://github.com/node-modules/utility/commit/7c102f4aff81e44b96629d692ce74f22cc02919f)] - test: travis support windows (#40) (fengmk2 <>) 104 | * [[`fdd2d27`](http://github.com/node-modules/utility/commit/fdd2d27ce99c25458795ad1e7555370498c051bd)] - chore: fix typo function name of sha256 (#39) (fengmk2 <>) 105 | 106 | 1.15.0 / 2018-09-12 107 | ================== 108 | 109 | **features** 110 | * [[`e3ae527`](http://github.com/node-modules/utility/commit/e3ae5277161e8870e097897f0dd41cd783170182)] - feat: add utility.unescape (#38) (Yiyu He <>) 111 | 112 | **others** 113 | * [[`50fb750`](http://github.com/node-modules/utility/commit/50fb750d0f24b7b47f0ef6e2ba5c42e5b2c7166a)] - chore: use ^ as deps version (#37) (fengmk2 <>) 114 | 115 | 1.14.0 / 2018-06-29 116 | ================== 117 | 118 | **features** 119 | * [[`d401917`](http://github.com/node-modules/utility/commit/d401917f20f89c52be237279d575c695f1bf6ae0)] - feat: add replacer and space to writeJSON* (#34) (Khaidi Chu <>) 120 | 121 | 1.13.1 / 2017-10-17 122 | ================== 123 | 124 | **fixes** 125 | * [[`fbbf905`](http://github.com/node-modules/utility/commit/fbbf905880185dce1e9cf980112a0f0b890e5969)] - fix: don't use arrow function (#31) (Yiyu He <>) 126 | 127 | 1.13.0 / 2017-10-17 128 | ================== 129 | 130 | **features** 131 | * [[`8a84707`](https://github.com/node-modules/utility.git/commit/8a847077b4d543193e4ca2f9ff69068a48d84909)] - feat: add readJSON and writeJSON (#29) (Haoliang Gao <>) 132 | 133 | 1.12.0 / 2017-04-19 134 | ================== 135 | 136 | * feat: add includesInvalidHttpHeaderChar() to detect invalid char 137 | * test: add url test for replaceInvalidHttpHeaderChar 138 | * chore: remove unused comments 139 | * feat: replacement support function format 140 | 141 | 1.11.0 / 2017-02-21 142 | ================== 143 | 144 | * feat: add utility.assign (#27) 145 | 146 | 1.10.0 / 2017-02-14 147 | ================== 148 | 149 | * feat: add replace invalid http header character (#26) 150 | 151 | 1.9.0 / 2016-11-14 152 | ================== 153 | 154 | * feat: add utils.random function (#25) 155 | * bench: add Array.from(arguments) bench test 156 | 157 | 1.8.0 / 2016-05-09 158 | ================== 159 | 160 | * feat(array): impl faster splice one element on array (#24) 161 | 162 | 1.7.1 / 2016-05-03 163 | ================== 164 | 165 | * refactor: use faster empty object instead of Object.create(null) (#23) 166 | 167 | 1.7.0 / 2016-04-07 168 | ================== 169 | 170 | * benchmark: update arguments to array 171 | * chore: add doc 172 | * feat: add utility.argumentsToArray 173 | * chore: add david-dm status badge 174 | * deps: remove unuse mm module 175 | * test: use ava and nyc instead of mocha and istanbul 176 | * bench: add string and tpl string benchmark 177 | 178 | 1.6.0 / 2015-12-04 179 | ================== 180 | 181 | * refactor: use escape-html 182 | 183 | 1.5.0 / 2015-11-25 184 | ================== 185 | 186 | * feat: utility.dig 187 | * test(date): fix timezone on test assert 188 | * feat(date): make YYYYMMDDHHmmss() support param not Date 189 | * test: use codecov.io 190 | 191 | 1.4.0 / 2015-05-22 192 | ================== 193 | 194 | * feat(JSON): add strict JSON parse 195 | 196 | 1.3.2 / 2015-05-08 197 | ================== 198 | 199 | * feat(crypto): add sha256 hash 200 | 201 | 1.3.1 / 2015-04-09 202 | ================== 203 | 204 | * fix(crypto): base64decode support return buffer 205 | 206 | 1.3.0 / 2015-01-31 207 | ================== 208 | 209 | * feat(string): add string replace 210 | 211 | 1.2.1 / 2014-11-14 212 | ================== 213 | 214 | * setImmediate support argmuents 215 | 216 | 1.2.0 / 2014-09-14 217 | ================== 218 | 219 | * add utility.try 220 | 221 | 1.1.0 / 2014-08-23 222 | ================== 223 | 224 | * add split(str, sep=,) 225 | 226 | 1.0.0 / 2014-08-01 227 | ================== 228 | 229 | * remove address methods, please use `address` module 230 | 231 | 0.1.16 / 2014-07-07 232 | ================== 233 | 234 | * fix deps 235 | 236 | 0.1.15 / 2014-07-07 237 | ================== 238 | 239 | * YYYYMMDDHHmmss() support custom dateSep 240 | 241 | 0.1.14 / 2014-06-25 242 | ================== 243 | 244 | * support `YYYYMMDD(d, sep)` 245 | * add YYYYMMDDHHmmss benchmark 246 | * add sha1 to readme 247 | * add random string benchmark 248 | 249 | 0.1.13 / 2014-04-24 250 | ================== 251 | 252 | * utils.YYYYMMDDHHmmssSSS(','); // '2013-04-17 14:43:02,674' 253 | 254 | 0.1.12 / 2014-04-03 255 | ================== 256 | 257 | * support var map = util.map({a: 1}) 258 | 259 | 0.1.11 / 2014-03-15 260 | ================== 261 | 262 | * add sha1() 263 | * remove config from scripts 264 | 265 | 0.1.10 / 2014-01-08 266 | ================== 267 | 268 | * add randomString() and has() (@dead-horse) 269 | * install from cnpm 270 | 271 | 0.1.9 / 2013-12-09 272 | ================== 273 | 274 | * add YYYYMMDD() 275 | 276 | 0.1.8 / 2013-11-25 277 | ================== 278 | 279 | * support sub object md5 280 | 281 | 0.1.7 / 2013-11-23 282 | ================== 283 | 284 | * support timestamp string 285 | 286 | 0.1.6 / 2013-11-23 287 | ================== 288 | 289 | * parse timestamp 290 | 291 | 0.1.5 / 2013-11-23 292 | ================== 293 | 294 | * hash object 295 | * add npm image 296 | 297 | 0.1.4 / 2013-11-16 298 | ================== 299 | 300 | * utils.setImmediate() 301 | * fix test case 302 | 303 | 0.1.3 / 2013-10-23 304 | ================== 305 | 306 | * add number utils: toSafeNumber() 307 | 308 | 0.1.2 / 2013-10-07 309 | ================== 310 | 311 | * add utils.YYYYMMDDHHmmss() 312 | 313 | 0.1.1 / 2013-09-23 314 | ================== 315 | 316 | * add base64 format md5 317 | 318 | 0.1.0 / 2013-09-03 319 | ================== 320 | 321 | * add timestamp() 322 | 323 | 0.0.13 / 2013-07-31 324 | ================== 325 | 326 | * move getIP() to address, fixed #2 327 | 328 | 0.0.12 / 2013-06-27 329 | ================== 330 | 331 | * utils.getParamNames(): get a function parameters names 332 | 333 | 0.0.11 / 2013-06-25 334 | ================== 335 | 336 | * fixed interface name wrong on liunx 337 | 338 | 0.0.10 / 2013-06-25 339 | ================== 340 | 341 | * add getIP() 342 | * add more test cases 343 | 344 | 0.0.9 / 2013-05-08 345 | ================== 346 | 347 | * Safe encodeURIComponent and decodeURIComponent 348 | 349 | 0.0.8 / 2013-05-06 350 | ================== 351 | 352 | * add randomSlice() fixed #1 353 | 354 | 0.0.7 / 2013-04-17 355 | ================== 356 | 357 | * fixed timezone +0000 and test cases 358 | 359 | 0.0.6 / 2013-04-17 360 | ================== 361 | 362 | * utils.logDate(); // '2013-04-17 14:43:02.674' 363 | 364 | 0.0.5 / 2013-04-16 365 | ================== 366 | 367 | * faster accesslogDate() impl 368 | * add benchmark 369 | 370 | 0.0.4 / 2013-04-16 371 | ================== 372 | 373 | * add accessLogDate() 374 | 375 | 0.0.3 / 2013-03-06 376 | ================== 377 | 378 | * add hmac() 379 | * update copyright year 380 | * update md5 readme 381 | 382 | 0.0.2 / 2013-01-31 383 | ================== 384 | 385 | * add base64 encode and urlsafe base64 encode 386 | * add html escape() 387 | * update makefile 388 | 389 | 0.0.1 / 2012-11-13 390 | ================== 391 | 392 | * first commit 393 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | This software is licensed under the MIT License. 2 | 3 | Copyright(c) 2012 - present node-modules and other contributors. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # utility 2 | 3 | [![NPM version][npm-image]][npm-url] 4 | [![CI](https://github.com/node-modules/utility/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/utility/actions/workflows/nodejs.yml) 5 | [![Test coverage][codecov-image]][codecov-url] 6 | [![npm download][download-image]][download-url] 7 | [![Node.js Version](https://img.shields.io/node/v/utility.svg?style=flat)](https://nodejs.org/en/download/) 8 | [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com) 9 | 10 | [npm-image]: https://img.shields.io/npm/v/utility.svg?style=flat-square 11 | [npm-url]: https://npmjs.org/package/utility 12 | [codecov-image]: https://codecov.io/github/node-modules/utility/coverage.svg?branch=master 13 | [codecov-url]: https://codecov.io/github/node-modules/utility?branch=master 14 | [download-image]: https://img.shields.io/npm/dm/utility.svg?style=flat-square 15 | [download-url]: https://npmjs.org/package/utility 16 | 17 | A collection of useful utilities. 18 | 19 | ## Install 20 | 21 | ```bash 22 | npm install utility 23 | ``` 24 | 25 | ## Usage 26 | 27 | ```js 28 | const utils = require('utility'); 29 | ``` 30 | 31 | Also you can use it within typescript, like this ↓ 32 | 33 | ```ts 34 | import * as utils from 'utility'; 35 | ``` 36 | 37 | ### md5 38 | 39 | ```ts 40 | import { md5 } from 'utility'; 41 | 42 | md5('苏千'); 43 | // '5f733c47c58a077d61257102b2d44481' 44 | 45 | md5(Buffer.from('苏千')); 46 | // '5f733c47c58a077d61257102b2d44481' 47 | 48 | // md5 base64 format 49 | md5('苏千', 'base64'); 50 | // 'X3M8R8WKB31hJXECstREgQ==' 51 | 52 | // Object md5 hash. Sorted by key, and JSON.stringify. See source code for detail 53 | md5({foo: 'bar', bar: 'foo'}).should.equal(md5({bar: 'foo', foo: 'bar'})); 54 | ``` 55 | 56 | ### sha1 57 | 58 | ```ts 59 | import { sha1 } from 'utility'; 60 | 61 | sha1('苏千'); 62 | // '0a4aff6bab634b9c2f99b71f25e976921fcde5a5' 63 | 64 | sha1(Buffer.from('苏千')); 65 | // '0a4aff6bab634b9c2f99b71f25e976921fcde5a5' 66 | 67 | // sha1 base64 format 68 | sha1('苏千', 'base64'); 69 | // 'Ckr/a6tjS5wvmbcfJel2kh/N5aU=' 70 | 71 | // Object sha1 hash. Sorted by key, and JSON.stringify. See source code for detail 72 | sha1({foo: 'bar', bar: 'foo'}).should.equal(sha1({bar: 'foo', foo: 'bar'})); 73 | ``` 74 | 75 | ### sha256 76 | 77 | ```ts 78 | import { sha256 } from 'utility'; 79 | 80 | sha256(Buffer.from('苏千')); 81 | // '75dd03e3fcdbba7d5bec07900bae740cc8e361d77e7df8949de421d3df5d3635' 82 | ``` 83 | 84 | ### hmac 85 | 86 | ```ts 87 | import { hmac } from 'utility'; 88 | 89 | // hmac-sha1 with base64 output encoding 90 | hmac('sha1', 'I am a key', 'hello world'); 91 | // 'pO6J0LKDxRRkvSECSEdxwKx84L0=' 92 | ``` 93 | 94 | ### decode and encode 95 | 96 | ```ts 97 | import { base64encode, base64decode, escape, unescape, encodeURIComponent, decodeURIComponent } from 'utility'; 98 | 99 | // base64 encode 100 | base64encode('你好¥'); 101 | // '5L2g5aW977+l' 102 | base64decode('5L2g5aW977+l'); 103 | // '你好¥' 104 | 105 | // urlsafe base64 encode 106 | base64encode('你好¥', true); 107 | // '5L2g5aW977-l' 108 | base64decode('5L2g5aW977-l', true); 109 | // '你好¥' 110 | 111 | // html escape and unescape 112 | escape('\'"""\ 13 | $ & & && & \ 14 | '; 15 | assert.equal(escape(unsafe), safe); 16 | }); 17 | }); 18 | 19 | describe('escape()', () => { 20 | it('unescape() should return html unsafe string', () => { 21 | const safe = '<script src="foo.js">"""</script>'"""\ 22 | $ & &amp; && &&nbsp;\ 23 | '; 24 | const unsafe = '\'"""\ 25 | $ & & && & \ 26 | '; 27 | assert.equal(utility.unescape(safe), unsafe); 28 | }); 29 | }); 30 | 31 | describe('encodeURIComponent(), decodeURIComponent()', () => { 32 | it('should encode and decode success', () => { 33 | const texts = [ 34 | 'foo', '中文', '数字', 35 | '%', 36 | String.fromCharCode(0xDFFF), // http://cnodejs.org/topic/4fd6b7ba839e1e581407aac8 37 | 123, 0, 1, Math.pow(2, 53), 38 | null, undefined, 39 | Buffer.from('中文水电费'), Buffer.alloc(100), 40 | ]; 41 | texts.forEach(text => { 42 | if (typeof text === 'string') { 43 | assert.equal(utility.decodeURIComponent(utility.encodeURIComponent(text)), text); 44 | } else { 45 | assert.equal(utility.decodeURIComponent(utility.encodeURIComponent(text as any)), String(text)); 46 | } 47 | }); 48 | }); 49 | 50 | it('should return source string when decode error', () => { 51 | assert.equal(utility.decodeURIComponent('%'), '%'); 52 | }); 53 | }); 54 | }); 55 | 56 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@eggjs/tsconfig", 3 | "compilerOptions": { 4 | "strict": true, 5 | "noImplicitAny": true, 6 | "target": "ES2022", 7 | "module": "NodeNext", 8 | "moduleResolution": "NodeNext" 9 | } 10 | } 11 | --------------------------------------------------------------------------------