mazey 5.9.2 → 5.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +142 -29
- package/README.zh-CN.md +48 -5
- package/lib/index.cjs.js +2 -2
- package/lib/index.d.ts +199 -86
- package/lib/index.esm.js +2 -2
- package/lib/mazey.min.js +2 -2
- package/package.json +6 -9
package/README.md
CHANGED
|
@@ -78,8 +78,12 @@ There are some examples maintained by hand below. For more information, please c
|
|
|
78
78
|
- [windowLoaded](#windowloaded)
|
|
79
79
|
- [Util](#util)
|
|
80
80
|
- [isNumber](#isnumber)
|
|
81
|
+
- [isNullish](#isnullish)
|
|
81
82
|
- [isJSONString](#isjsonstring)
|
|
82
83
|
- [parseJsonSafe](#parsejsonsafe)
|
|
84
|
+
- [isCNMobileNumber](#iscnmobilenumber)
|
|
85
|
+
- [escapeHTML and unescapeHTML](#escapehtml-and-unescapehtml)
|
|
86
|
+
- [truncateByWeightedLength](#truncatebyweightedlength)
|
|
83
87
|
- [escapeHtmlAttribute](#escapehtmlattribute)
|
|
84
88
|
- [sha256Hex](#sha256hex)
|
|
85
89
|
- [isValidData](#isvaliddata)
|
|
@@ -108,6 +112,7 @@ There are some examples maintained by hand below. For more information, please c
|
|
|
108
112
|
- [toJavaScriptGlobalName](#tojavascriptglobalname)
|
|
109
113
|
- [derivePackageMetadata](#derivepackagemetadata)
|
|
110
114
|
- [URL](#url)
|
|
115
|
+
- [getURLPathExtension](#geturlpathextension)
|
|
111
116
|
- [getQueryParam](#getqueryparam)
|
|
112
117
|
- [getUrlParam](#geturlparam)
|
|
113
118
|
- [getHashQueryParam](#gethashqueryparam)
|
|
@@ -121,19 +126,19 @@ There are some examples maintained by hand below. For more information, please c
|
|
|
121
126
|
- [Storage Helpers](#storage-helpers)
|
|
122
127
|
- [DOM](#dom)
|
|
123
128
|
- [Class Helpers](#class-helpers)
|
|
124
|
-
- [
|
|
129
|
+
- [hideElements and showElements](#hideelements-and-showelements)
|
|
125
130
|
- [isValidCssSelector](#isvalidcssselector)
|
|
126
131
|
- [resolveElementTarget](#resolveelementtarget)
|
|
127
132
|
- [extractElementText](#extractelementtext)
|
|
128
133
|
- [injectStyle](#injectstyle)
|
|
129
|
-
- [
|
|
134
|
+
- [createCSSRule](#createcssrule)
|
|
130
135
|
- [newLine](#newline)
|
|
131
136
|
- [Event](#event)
|
|
132
137
|
- [onEvent](#onevent)
|
|
133
138
|
- [Calculate and Formula](#calculate-and-formula)
|
|
134
139
|
- [calculateAspectRatio](#calculateaspectratio)
|
|
135
140
|
- [calculateCAGR](#calculatecagr)
|
|
136
|
-
- [
|
|
141
|
+
- [randomBoolean](#randomboolean)
|
|
137
142
|
- [longestComSubstring](#longestcomsubstring)
|
|
138
143
|
- [longestComSubsequence](#longestcomsubsequence)
|
|
139
144
|
- [Browser Information](#browser-information)
|
|
@@ -152,6 +157,7 @@ There are some examples maintained by hand below. For more information, please c
|
|
|
152
157
|
- [isWindows](#iswindows)
|
|
153
158
|
- [isLinux](#islinux)
|
|
154
159
|
- [getBrowserInfo](#getbrowserinfo)
|
|
160
|
+
- [getBrowserClassNames](#getbrowserclassnames)
|
|
155
161
|
- [isSafePWAEnv](#issafepwaenv)
|
|
156
162
|
- [isStandalonePWA](#isstandalonepwa)
|
|
157
163
|
- [listenMediaQueryChanges](#listenmediaquerychanges)
|
|
@@ -293,6 +299,9 @@ Load Success: load
|
|
|
293
299
|
|
|
294
300
|
### Util
|
|
295
301
|
|
|
302
|
+
Use native `Date.now()` to get the current epoch time in milliseconds. The
|
|
303
|
+
former `mNow()` helper remains available as a deprecated compatibility API.
|
|
304
|
+
|
|
296
305
|
#### isNumber
|
|
297
306
|
|
|
298
307
|
Check whether a value is an allowed numeric primitive. Optional constraints can
|
|
@@ -323,6 +332,21 @@ true false false true false true true false
|
|
|
323
332
|
reversed bounds return `false`. Existing non-finite-number behavior is unchanged
|
|
324
333
|
when `integer`, `min`, and `max` are omitted.
|
|
325
334
|
|
|
335
|
+
#### isNullish
|
|
336
|
+
|
|
337
|
+
Check whether a value is exactly `undefined` or `null`. Other falsy values are
|
|
338
|
+
not nullish.
|
|
339
|
+
|
|
340
|
+
```javascript
|
|
341
|
+
isNullish(undefined); // true
|
|
342
|
+
isNullish(null); // true
|
|
343
|
+
isNullish(false); // false
|
|
344
|
+
isNullish(0); // false
|
|
345
|
+
isNullish(""); // false
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
`isUdfOrNul` remains available as a deprecated alias of `isNullish`.
|
|
349
|
+
|
|
326
350
|
#### isJSONString
|
|
327
351
|
|
|
328
352
|
Check whether it is a valid JSON string.
|
|
@@ -354,6 +378,55 @@ const fallback = parseJsonSafe("invalid", {});
|
|
|
354
378
|
console.log(data, fallback);
|
|
355
379
|
```
|
|
356
380
|
|
|
381
|
+
#### isCNMobileNumber
|
|
382
|
+
|
|
383
|
+
Check the Chinese mobile-number format: `1` followed by ten digits. This checks
|
|
384
|
+
format only, without verifying assigned prefixes, ownership, or reachability.
|
|
385
|
+
|
|
386
|
+
```javascript
|
|
387
|
+
import { isCNMobileNumber } from "mazey";
|
|
388
|
+
|
|
389
|
+
isCNMobileNumber("13800138000"); // true
|
|
390
|
+
isCNMobileNumber("+8613800138000"); // false
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
`isValidPhoneNumber` and `isMobile` remain deprecated aliases.
|
|
394
|
+
|
|
395
|
+
#### escapeHTML and unescapeHTML
|
|
396
|
+
|
|
397
|
+
Escape six HTML-sensitive characters or decode that fixed entity set in one
|
|
398
|
+
pass. These helpers do not sanitize arbitrary HTML or validate URLs.
|
|
399
|
+
`unescapeHTML` preserves unrecognized entities.
|
|
400
|
+
|
|
401
|
+
```javascript
|
|
402
|
+
import { escapeHTML, unescapeHTML } from "mazey";
|
|
403
|
+
|
|
404
|
+
escapeHTML('<b title="x">A&B</b>');
|
|
405
|
+
// '<b title="x">A&B</b>'
|
|
406
|
+
unescapeHTML("<b>A&B</b>");
|
|
407
|
+
// "<b>A&B</b>"
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
`sanitizeInput` and `unsanitizeInput` remain deprecated aliases. The legacy
|
|
411
|
+
`unsanitize` alias also remains available.
|
|
412
|
+
|
|
413
|
+
#### truncateByWeightedLength
|
|
414
|
+
|
|
415
|
+
Truncate by weighted UTF-16 length: code units from `U+0000` through `U+00FF`
|
|
416
|
+
count as one; all other code units count as two. Optional truncation text is
|
|
417
|
+
appended after the limit. This does not measure bytes or rendered width and can
|
|
418
|
+
split surrogate pairs.
|
|
419
|
+
|
|
420
|
+
```javascript
|
|
421
|
+
import { truncateByWeightedLength } from "mazey";
|
|
422
|
+
|
|
423
|
+
truncateByWeightedLength("Hello世界", 7); // "Hello世"
|
|
424
|
+
truncateByWeightedLength("Hello世界", 7, { hasDot: true }); // "Hello世..."
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
`cutZHString` remains a deprecated alias. The legacy `truncateZHString` and
|
|
428
|
+
`cutCHSString` signatures retain their boolean `hasDot` argument.
|
|
429
|
+
|
|
357
430
|
#### escapeHtmlAttribute
|
|
358
431
|
|
|
359
432
|
Escape text for use inside a quoted HTML attribute without escaping `/`.
|
|
@@ -893,6 +966,23 @@ manifest.
|
|
|
893
966
|
|
|
894
967
|
### URL
|
|
895
968
|
|
|
969
|
+
#### getURLPathExtension
|
|
970
|
+
|
|
971
|
+
Extract a file extension from a URL or path, excluding query and fragment text.
|
|
972
|
+
This helper does not detect MIME types or inspect file contents.
|
|
973
|
+
It preserves the existing string-based behavior: `archive.tar.gz` returns
|
|
974
|
+
`tar.gz`, and an origin-only input such as `https://example.com` returns `com`.
|
|
975
|
+
|
|
976
|
+
```javascript
|
|
977
|
+
import { getURLPathExtension } from "mazey";
|
|
978
|
+
|
|
979
|
+
getURLPathExtension("https://example.com/image.png?width=200#preview"); // "png"
|
|
980
|
+
getURLPathExtension("/images/photo.jpg"); // "jpg"
|
|
981
|
+
getURLPathExtension("/images/photo"); // ""
|
|
982
|
+
```
|
|
983
|
+
|
|
984
|
+
`getUrlFileType` remains a deprecated alias.
|
|
985
|
+
|
|
896
986
|
#### getQueryParam
|
|
897
987
|
|
|
898
988
|
Get the query param's value of the current Web URL(`location.search`).
|
|
@@ -1139,29 +1229,31 @@ addClass(dom, "test");
|
|
|
1139
1229
|
removeClass(dom, "test");
|
|
1140
1230
|
```
|
|
1141
1231
|
|
|
1142
|
-
####
|
|
1232
|
+
#### hideElements and showElements
|
|
1143
1233
|
|
|
1144
1234
|
Hide or show a CSS selector, one element, or an iterable or array-like element
|
|
1145
1235
|
collection. Both helpers return the original target, so a caller can retain its
|
|
1146
1236
|
own chaining convention. Duplicate elements are changed only once, and invalid
|
|
1147
1237
|
selectors or unsupported values are ignored.
|
|
1148
1238
|
|
|
1149
|
-
`
|
|
1150
|
-
that value, or recovers the element's normal display
|
|
1151
|
-
otherwise keep it hidden.
|
|
1239
|
+
`hideElements()` preserves a visible element's inline `display` value.
|
|
1240
|
+
`showElements()` restores that value, or recovers the element's normal display
|
|
1241
|
+
when a stylesheet would otherwise keep it hidden.
|
|
1152
1242
|
|
|
1153
1243
|
```javascript
|
|
1154
|
-
import {
|
|
1244
|
+
import { hideElements, showElements } from "mazey";
|
|
1155
1245
|
|
|
1156
1246
|
const notices = document.querySelectorAll(".notice");
|
|
1157
1247
|
|
|
1158
|
-
|
|
1159
|
-
|
|
1248
|
+
hideElements(notices);
|
|
1249
|
+
showElements(notices);
|
|
1160
1250
|
|
|
1161
|
-
|
|
1162
|
-
|
|
1251
|
+
hideElements("#temporary-message");
|
|
1252
|
+
showElements(document.querySelector("#temporary-message"));
|
|
1163
1253
|
```
|
|
1164
1254
|
|
|
1255
|
+
`hide` and `show` remain available as deprecated aliases.
|
|
1256
|
+
|
|
1165
1257
|
#### isValidCssSelector
|
|
1166
1258
|
|
|
1167
1259
|
Check whether a value is a CSS selector supported by the supplied query root.
|
|
@@ -1243,18 +1335,18 @@ Output:
|
|
|
1243
1335
|
<style>body { background-color: #444; }</style>
|
|
1244
1336
|
```
|
|
1245
1337
|
|
|
1246
|
-
Example 3: Combine `
|
|
1338
|
+
Example 3: Combine `createCSSRule` and `injectStyle` to add multiple styles at once.
|
|
1247
1339
|
|
|
1248
1340
|
```javascript
|
|
1249
|
-
import {
|
|
1341
|
+
import { createCSSRule, injectStyle } from "mazey";
|
|
1250
1342
|
|
|
1251
|
-
const xStyle =
|
|
1343
|
+
const xStyle = createCSSRule(
|
|
1252
1344
|
".footer>.x-wish>a:first-child" +
|
|
1253
1345
|
",div.wish-flex>a[href^='https://github.com/chengchuu']" +
|
|
1254
1346
|
",.m-hide",
|
|
1255
1347
|
[ "display: none" ]
|
|
1256
1348
|
);
|
|
1257
|
-
const yStyle =
|
|
1349
|
+
const yStyle = createCSSRule(
|
|
1258
1350
|
".footer>.y-wish:before",
|
|
1259
1351
|
[
|
|
1260
1352
|
`content: 'Copyright (c) chengchuu'`,
|
|
@@ -1274,15 +1366,19 @@ Output:
|
|
|
1274
1366
|
<style id="z-style">.footer>.x-wish>a:first-child,div.wish-flex>a[href^='https://github.com/chengchuu'],.m-hide{display: none;}.footer>.y-wish:before{content: 'Copyright (c) chengchuu';color: inherit;padding-inline-start: var(--y-wish-1_5);padding-inline-end: var(--y-wish-1_5);padding-top: var(--y-wish-1);padding-bottom: var(--y-wish-1);}</style>
|
|
1275
1367
|
```
|
|
1276
1368
|
|
|
1277
|
-
####
|
|
1369
|
+
#### createCSSRule
|
|
1278
1370
|
|
|
1279
|
-
|
|
1371
|
+
Create CSS rule text from a selector and an array of declarations. The helper
|
|
1372
|
+
joins declarations with semicolons without validating or escaping CSS.
|
|
1373
|
+
`genStyleString` remains a deprecated alias.
|
|
1280
1374
|
|
|
1281
1375
|
Usage:
|
|
1282
1376
|
|
|
1283
1377
|
```javascript
|
|
1284
|
-
|
|
1285
|
-
|
|
1378
|
+
import { createCSSRule } from "mazey";
|
|
1379
|
+
|
|
1380
|
+
const ret1 = createCSSRule(".a", [ "color:red" ]);
|
|
1381
|
+
const ret2 = createCSSRule("#b", [ "color:red", "font-size:12px" ]);
|
|
1286
1382
|
console.log(ret1);
|
|
1287
1383
|
console.log(ret2);
|
|
1288
1384
|
```
|
|
@@ -1294,18 +1390,18 @@ Output:
|
|
|
1294
1390
|
#b{color:red;font-size:12px;}
|
|
1295
1391
|
```
|
|
1296
1392
|
|
|
1297
|
-
Example: Combine `
|
|
1393
|
+
Example: Combine `createCSSRule` and `injectStyle` to add multiple styles at once.
|
|
1298
1394
|
|
|
1299
1395
|
```javascript
|
|
1300
|
-
import {
|
|
1396
|
+
import { createCSSRule, injectStyle } from "mazey";
|
|
1301
1397
|
|
|
1302
|
-
const xStyle =
|
|
1398
|
+
const xStyle = createCSSRule(
|
|
1303
1399
|
".footer>.x-wish>a:first-child" +
|
|
1304
1400
|
",div.wish-flex>a[href^='https://github.com/chengchuu']" +
|
|
1305
1401
|
",.m-hide",
|
|
1306
1402
|
[ "display: none" ]
|
|
1307
1403
|
);
|
|
1308
|
-
const yStyle =
|
|
1404
|
+
const yStyle = createCSSRule(
|
|
1309
1405
|
".footer>.y-wish:before",
|
|
1310
1406
|
[
|
|
1311
1407
|
`content: 'Copyright (c) chengchuu'`,
|
|
@@ -1435,14 +1531,14 @@ calculateCAGR(
|
|
|
1435
1531
|
|
|
1436
1532
|
Date strings are validated using Mazey's strict date rules. Invalid dates, malformed or non-finite returns, and non-increasing date ranges throw errors. The parsed total return must be greater than `-1`, because `-1` represents a complete loss for which CAGR is undefined.
|
|
1437
1533
|
|
|
1438
|
-
####
|
|
1534
|
+
#### randomBoolean
|
|
1439
1535
|
|
|
1440
|
-
|
|
1536
|
+
Return whether a generated random value is less than the supplied probability.
|
|
1441
1537
|
|
|
1442
1538
|
Usage:
|
|
1443
1539
|
|
|
1444
1540
|
```javascript
|
|
1445
|
-
const ret =
|
|
1541
|
+
const ret = randomBoolean(0.5); // A 50% chance of returning true.
|
|
1446
1542
|
console.log(ret);
|
|
1447
1543
|
```
|
|
1448
1544
|
|
|
@@ -1459,7 +1555,7 @@ Example: Test the precision.
|
|
|
1459
1555
|
let trueCount = 0;
|
|
1460
1556
|
let falseCount = 0;
|
|
1461
1557
|
new Array(1000000).fill(0).forEach(() => {
|
|
1462
|
-
if (
|
|
1558
|
+
if (randomBoolean(0.5)) {
|
|
1463
1559
|
trueCount++;
|
|
1464
1560
|
} else {
|
|
1465
1561
|
falseCount++;
|
|
@@ -1468,6 +1564,9 @@ new Array(1000000).fill(0).forEach(() => {
|
|
|
1468
1564
|
console.log(trueCount, falseCount); // 499994 500006
|
|
1469
1565
|
```
|
|
1470
1566
|
|
|
1567
|
+
`randomBoolean` evaluates `Math.random() < rate` without clamping the supplied
|
|
1568
|
+
rate. `isHit` is a deprecated alias, and `inRate` remains a compatibility alias.
|
|
1569
|
+
|
|
1471
1570
|
#### longestComSubstring
|
|
1472
1571
|
|
|
1473
1572
|
Computes the longest common substring of two strings.
|
|
@@ -1791,7 +1890,7 @@ reports iOS and Android as `"mobile"`; the new helpers provide a more specific
|
|
|
1791
1890
|
phone, tablet, or desktop classification.
|
|
1792
1891
|
|
|
1793
1892
|
`isPhone` checks device form factor. The separate `isMobile` API is a direct
|
|
1794
|
-
alias of `
|
|
1893
|
+
alias of `isCNMobileNumber`; it validates an 11-digit Chinese mobile-shaped
|
|
1795
1894
|
number and does not inspect the browser or device.
|
|
1796
1895
|
|
|
1797
1896
|
#### isIOS
|
|
@@ -1906,6 +2005,20 @@ const { system, shell } = getBrowserInfo();
|
|
|
1906
2005
|
const isMobileQQ = ["android", "ios"].includes(system) && ["qq_browser", "qq_app"].includes(shell);
|
|
1907
2006
|
```
|
|
1908
2007
|
|
|
2008
|
+
#### getBrowserClassNames
|
|
2009
|
+
|
|
2010
|
+
Return class-name tokens from the cached browser classification. An optional
|
|
2011
|
+
prefix and separator apply to each token; the helper does not modify the DOM.
|
|
2012
|
+
|
|
2013
|
+
```javascript
|
|
2014
|
+
import { getBrowserClassNames } from "mazey";
|
|
2015
|
+
|
|
2016
|
+
console.log(getBrowserClassNames("browser"));
|
|
2017
|
+
// Possible output: ["browser-windows", "browser-desktop", "browser-webkit", "browser-chrome"]
|
|
2018
|
+
```
|
|
2019
|
+
|
|
2020
|
+
`genBrowserAttrs` remains a deprecated alias.
|
|
2021
|
+
|
|
1909
2022
|
#### isSafePWAEnv
|
|
1910
2023
|
|
|
1911
2024
|
Detect whether the current browser document provides the minimum prerequisites
|
package/README.zh-CN.md
CHANGED
|
@@ -78,6 +78,7 @@ isNumber(z, { isInfinityAsNumber: true }); // 输出: true
|
|
|
78
78
|
- [windowLoaded](#windowloaded)
|
|
79
79
|
- [通用工具](#通用工具)
|
|
80
80
|
- [isNumber](#isnumber)
|
|
81
|
+
- [isNullish](#isnullish)
|
|
81
82
|
- [isJSONString](#isjsonstring)
|
|
82
83
|
- [isValidData](#isvaliddata)
|
|
83
84
|
- [genRndNumString](#genrndnumstring)
|
|
@@ -107,6 +108,7 @@ isNumber(z, { isInfinityAsNumber: true }); // 输出: true
|
|
|
107
108
|
- [Storage 工具](#storage-工具)
|
|
108
109
|
- [DOM](#dom)
|
|
109
110
|
- [Class 工具](#class-工具)
|
|
111
|
+
- [hideElements 和 showElements](#hideelements-和-showelements)
|
|
110
112
|
- [injectStyle](#injectstyle)
|
|
111
113
|
- [genStyleString](#genstylestring)
|
|
112
114
|
- [newLine](#newline)
|
|
@@ -115,7 +117,7 @@ isNumber(z, { isInfinityAsNumber: true }); // 输出: true
|
|
|
115
117
|
- [计算与公式](#计算与公式)
|
|
116
118
|
- [calculateAspectRatio](#calculateaspectratio)
|
|
117
119
|
- [calculateCAGR](#calculatecagr)
|
|
118
|
-
- [
|
|
120
|
+
- [randomBoolean](#randomboolean)
|
|
119
121
|
- [longestComSubstring](#longestcomsubstring)
|
|
120
122
|
- [longestComSubsequence](#longestcomsubsequence)
|
|
121
123
|
- [浏览器信息](#浏览器信息)
|
|
@@ -266,6 +268,9 @@ windowLoaded()
|
|
|
266
268
|
|
|
267
269
|
### 通用工具
|
|
268
270
|
|
|
271
|
+
使用原生 `Date.now()` 获取当前时间戳 (毫秒)。原有的 `mNow()` 工具仍作为弃用兼容 API
|
|
272
|
+
保留。
|
|
273
|
+
|
|
269
274
|
#### isNumber
|
|
270
275
|
|
|
271
276
|
判断某个值是否为有效数字。
|
|
@@ -289,6 +294,20 @@ console.log(ret1, ret2, ret3, ret4, ret5, ret6);
|
|
|
289
294
|
true false false true false true
|
|
290
295
|
```
|
|
291
296
|
|
|
297
|
+
#### isNullish
|
|
298
|
+
|
|
299
|
+
判断值是否严格为 `undefined` 或 `null`。其他假值不属于空值。
|
|
300
|
+
|
|
301
|
+
```javascript
|
|
302
|
+
isNullish(undefined); // true
|
|
303
|
+
isNullish(null); // true
|
|
304
|
+
isNullish(false); // false
|
|
305
|
+
isNullish(0); // false
|
|
306
|
+
isNullish(""); // false
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
`isUdfOrNul` 仍作为 `isNullish` 的弃用别名保留。
|
|
310
|
+
|
|
292
311
|
#### isJSONString
|
|
293
312
|
|
|
294
313
|
判断字符串是否为有效的 JSON 字符串。
|
|
@@ -861,6 +880,27 @@ addClass(dom, "test");
|
|
|
861
880
|
removeClass(dom, "test");
|
|
862
881
|
```
|
|
863
882
|
|
|
883
|
+
#### hideElements 和 showElements
|
|
884
|
+
|
|
885
|
+
隐藏或显示 CSS 选择器、单个元素、可迭代元素集合或类数组元素集合。两个函数都会返回原始输入。
|
|
886
|
+
重复元素只会被修改一次。函数会忽略无效选择器和不支持的值。
|
|
887
|
+
|
|
888
|
+
`hideElements()` 会保存可见元素的内联 `display` 值。`showElements()` 会恢复该值。如果样式表仍隐藏该元素,函数会恢复元素的默认显示方式。
|
|
889
|
+
|
|
890
|
+
```javascript
|
|
891
|
+
import { hideElements, showElements } from "mazey";
|
|
892
|
+
|
|
893
|
+
const notices = document.querySelectorAll(".notice");
|
|
894
|
+
|
|
895
|
+
hideElements(notices);
|
|
896
|
+
showElements(notices);
|
|
897
|
+
|
|
898
|
+
hideElements("#temporary-message");
|
|
899
|
+
showElements(document.querySelector("#temporary-message"));
|
|
900
|
+
```
|
|
901
|
+
|
|
902
|
+
`hide` 和 `show` 仍作为弃用别名保留。
|
|
903
|
+
|
|
864
904
|
#### injectStyle
|
|
865
905
|
|
|
866
906
|
在 `<head>` 中添加 `<style>` 元素。
|
|
@@ -1092,14 +1132,14 @@ calculateCAGR(
|
|
|
1092
1132
|
|
|
1093
1133
|
日期字符串遵循 Mazey 的严格日期校验规则。无效日期、格式错误或非有限的回报率,以及没有递增的日期范围都会抛出错误。解析后的总回报率必须大于 `-1`,因为 `-1` 表示本金完全损失,此时 CAGR 没有定义。
|
|
1094
1134
|
|
|
1095
|
-
####
|
|
1135
|
+
#### randomBoolean
|
|
1096
1136
|
|
|
1097
|
-
|
|
1137
|
+
判断生成的随机值是否小于指定概率。
|
|
1098
1138
|
|
|
1099
1139
|
用法:
|
|
1100
1140
|
|
|
1101
1141
|
```javascript
|
|
1102
|
-
const ret =
|
|
1142
|
+
const ret = randomBoolean(0.5); // 有 50% 的概率返回 true
|
|
1103
1143
|
console.log(ret);
|
|
1104
1144
|
```
|
|
1105
1145
|
|
|
@@ -1116,7 +1156,7 @@ true
|
|
|
1116
1156
|
let trueCount = 0;
|
|
1117
1157
|
let falseCount = 0;
|
|
1118
1158
|
new Array(1000000).fill(0).forEach(() => {
|
|
1119
|
-
if (
|
|
1159
|
+
if (randomBoolean(0.5)) {
|
|
1120
1160
|
trueCount++;
|
|
1121
1161
|
} else {
|
|
1122
1162
|
falseCount++;
|
|
@@ -1125,6 +1165,9 @@ new Array(1000000).fill(0).forEach(() => {
|
|
|
1125
1165
|
console.log(trueCount, falseCount); // 499994 500006
|
|
1126
1166
|
```
|
|
1127
1167
|
|
|
1168
|
+
`randomBoolean` 直接计算 `Math.random() < rate`,不会限制传入的概率值。`isHit` 是弃用别名。
|
|
1169
|
+
`inRate` 仍作为兼容别名保留。
|
|
1170
|
+
|
|
1128
1171
|
#### longestComSubstring
|
|
1129
1172
|
|
|
1130
1173
|
计算两个字符串的最长公共子串长度。
|