-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90d54af
commit 472ed5a
Showing
48 changed files
with
707 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
i18n/zh/docusaurus-plugin-content-docs/current/bifurcate.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
title: bifurcate | ||
tags: [array, intermediate] | ||
author_title: Deepak Vishwakarma | ||
author_url: https://github.com/deepakshrma | ||
author_image_url: https://avatars2.githubusercontent.com/u/7682731?s=400 | ||
description: 在 TypeScript、JavaScript 和 Deno 中实现 "bifurcate" 的方法。 | ||
image: https://www.positronx.io/wp-content/uploads/2018/11/positronx-banner-1152-1.jpg | ||
--- | ||
|
||
 | ||
 | ||
 | ||
|
||
将值分成两组。如果 `filter` 中的元素为真值,则集合中对应的元素属于第一组;否则,它属于第二组。 | ||
|
||
使用 `Array.prototype.reduce()` 和 `Array.prototype.push()` 根据 `filter` 将元素添加到组中。 | ||
|
||
```ts title="typescript" | ||
const bifurcate = <T = any>(arr: T[], filter: boolean[]) => | ||
arr.reduce( | ||
(acc, val, i) => { | ||
acc[filter[i] ? 0 : 1].push(val); | ||
return acc; | ||
}, | ||
[[] as T[], [] as T[]] | ||
); | ||
``` | ||
|
||
```ts title="typescript" | ||
bifurcate(["beep", "boop", "foo", "bar"], [true, true, false, true]); // [ ['beep', 'boop', 'bar'], ['foo'] ] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
i18n/zh/docusaurus-plugin-content-docs/current/colorize.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
title: colorize [给文本着色] | ||
tags: [node, string, intermediate] | ||
author_title: Deepak Vishwakarma | ||
author_url: https://github.com/deepakshrma | ||
author_image_url: https://avatars2.githubusercontent.com/u/7682731?s=400 | ||
description: 在 TypeScript、JavaScript 和 Deno 中实现 "colorize" 的方法。 | ||
image: https://www.positronx.io/wp-content/uploads/2018/11/positronx-banner-1152-1.jpg | ||
--- | ||
|
||
 | ||
 | ||
 | ||
|
||
在控制台中添加特殊字符到文本中以彩色打印(与 `console.log()` 结合使用)。 | ||
|
||
使用模板文字和特殊字符为字符串输出添加适当的颜色代码。 | ||
对于背景颜色,添加一个在字符串末尾重置背景颜色的特殊字符。 | ||
|
||
```ts title="typescript" | ||
export const colorize = new (class { | ||
color = (code: number, ended = false, ...messages: any[]) => | ||
`\x1b[${code}m${messages.join(" ")}${ended ? "\x1b[0m" : ""}`; | ||
black = this.color.bind(null, 30, false); | ||
red = this.color.bind(null, 31, false); | ||
green = this.color.bind(null, 32, false); | ||
yellow = this.color.bind(this, 33, false); | ||
blue = this.color.bind(this, 34, false); | ||
magenta = this.color.bind(this, 35, false); | ||
cyan = this.color.bind(this, 36, false); | ||
white = this.color.bind(this, 37, false); | ||
bgBlack = this.color.bind(this, 40, true); | ||
bgRed = this.color.bind(this, 41, true); | ||
bgGreen = this.color.bind(this, 42, true); | ||
bgYellow = this.color.bind(this, 43, true); | ||
bgBlue = this.color.bind(this, 44, true); | ||
bgMagenta = this.color.bind(this, 45, true); | ||
bgCyan = this.color.bind(this, 46, true); | ||
bgWhite = this.color.bind(this, 47, true); | ||
})(); | ||
|
||
const color = colorize; | ||
``` | ||
|
||
```ts title="typescript" | ||
console.log(color.red("foo")); // 'foo' (red letters) | ||
console.log(color.bgBlue("foo", "bar")); // 'foo bar' (blue background) | ||
console.log(color.bgWhite(color.yellow("foo"), color.green("foo"))); // 'foo bar' (first | ||
//word in yellow letters, second word in green letters, white background for both) | ||
|
||
console.log(colorize.red("foo")); // 'foo' (red letters) | ||
console.log(colorize.bgBlue("foo", "bar")); // 'foo bar' (blue background) | ||
console.log(colorize.bgWhite(colorize.yellow("foo"), colorize.green("foo"))); // 'foo bar' (first | ||
//word in yellow letters, second word in green letters, white background for both) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
title: compact [压缩] | ||
tags: [array, beginner] | ||
author_title: Deepak Vishwakarma | ||
author_url: https://github.com/deepakshrma | ||
author_image_url: https://avatars2.githubusercontent.com/u/7682731?s=400 | ||
description: 在 TypeScript、JavaScript 和 Deno 中实现 "compact" 的方法。 | ||
image: https://www.positronx.io/wp-content/uploads/2018/11/positronx-banner-1152-1.jpg | ||
--- | ||
|
||
 | ||
 | ||
 | ||
|
||
从数组中移除假值。 | ||
|
||
使用 `Array.prototype.filter()` 来过滤掉假值 `(false、null、0、""、undefined 和 NaN)`。 | ||
|
||
```ts title="typescript" | ||
const compact = (arr: any[]) => arr.filter(Boolean); | ||
``` | ||
|
||
```ts title="typescript" | ||
compact([0, 1, false, 2, "", 3, "a", Number("e") * 23, NaN, "s", 34]); // [ 1, 2, 3, 'a', 's', 34 ] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
title: defer [推迟] | ||
tags: [function, intermediate] | ||
author_title: Deepak Vishwakarma | ||
author_url: https://github.com/deepakshrma | ||
author_image_url: https://avatars2.githubusercontent.com/u/7682731?s=400 | ||
description: 在 TypeScript、JavaScript 和 Deno 中实现 "defer" 的方法。 | ||
image: https://www.positronx.io/wp-content/uploads/2018/11/positronx-banner-1152-1.jpg | ||
--- | ||
|
||
 | ||
 | ||
|
||
延迟调用一个函数,直到当前调用栈已经清空。 | ||
|
||
使用 `setTimeout()`,设置超时为 1 毫秒,向浏览器事件队列中添加一个新事件,以便让渲染引擎完成其工作。使用扩展运算符 (`...`) 来向函数提供任意数量的参数。 | ||
|
||
```js | ||
const defer = (fn, ...args) => setTimeout(fn, 1, ...args); | ||
``` | ||
|
||
```js | ||
// Example A: | ||
defer(console.log, "a"), console.log("b"); // logs 'b' then 'a' | ||
|
||
// Example B: | ||
document.querySelector("#someElement").innerHTML = "Hello"; | ||
longRunningFunction(); // Browser will not update the HTML until this has finished | ||
defer(longRunningFunction); // Browser will update the HTML then run the function | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- | ||
title: delay [延迟] | ||
tags: [function, intermediate] | ||
author_title: Deepak Vishwakarma | ||
author_url: https://github.com/deepakshrma | ||
author_image_url: https://avatars2.githubusercontent.com/u/7682731?s=400 | ||
description: 在 TypeScript、JavaScript 和 Deno 中实现 "delay" 的方法。 | ||
image: https://www.positronx.io/wp-content/uploads/2018/11/positronx-banner-1152-1.jpg | ||
--- | ||
|
||
 | ||
 | ||
 | ||
|
||
在 `wait` 毫秒后调用提供的函数。 | ||
|
||
使用 `setTimeout()` 来延迟执行 fn。 | ||
|
||
使用扩展运算符 `(...)` 来向函数提供任意数量的参数。 | ||
|
||
```ts title="typescript" | ||
const delay = (fn: Func, wait: number, ...args: any[]) => | ||
setTimeout(fn, wait, ...args); | ||
|
||
// Return a promise, Resolve after `wait` milliseconds. | ||
const delayedPromise = (wait: number = 300, ...args: any[]) => | ||
new Promise((resolve) => { | ||
delay(resolve, wait, ...args); | ||
}); | ||
``` | ||
|
||
```ts title="typescript" | ||
delay( | ||
function (text) { | ||
console.log(text); | ||
}, | ||
1000, | ||
"later" | ||
); // Logs 'later' after one second. | ||
|
||
// delayedPromise | ||
let counter = 0; | ||
const updateState = () => { | ||
counter++; | ||
}; | ||
const debouncedUpdate = debounce(updateState); | ||
debouncedUpdate(); // counter == 1 | ||
debouncedUpdate(); // counter == 1 | ||
await delayedPromise(); // counter == 1 | ||
assertEquals(counter, 1); | ||
``` |
46 changes: 46 additions & 0 deletions
46
i18n/zh/docusaurus-plugin-content-docs/current/ellipsis.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
title: ellipsis [省略号] | ||
tags: [string, beginner] | ||
author_title: Deepak Vishwakarma | ||
author_url: https://github.com/deepakshrma | ||
author_image_url: https://avatars2.githubusercontent.com/u/7682731?s=400 | ||
description: 在 TypeScript、JavaScript 和 Deno 中实现 "ellipsis" 的方法。 | ||
image: https://www.positronx.io/wp-content/uploads/2018/11/positronx-banner-1152-1.jpg | ||
--- | ||
|
||
 | ||
 | ||
|
||
将字符串截断到指定长度。 | ||
|
||
判断字符串的 `length` 是否大于 num。 | ||
|
||
返回截断到所需长度的字符串,并在末尾添加 `'...'`,或者返回原始字符串。 | ||
|
||
```ts title="typescript" | ||
const truncateString = ( | ||
str: string, | ||
num: number = str.length, | ||
ellipsisStr = "..." | ||
) => | ||
str.length >= num | ||
? str.slice(0, num >= ellipsisStr.length ? num - ellipsisStr.length : num) + | ||
ellipsisStr | ||
: str; | ||
|
||
const ellipsis = (str: string, num: number = str.length, ellipsisStr = "...") => | ||
str.length >= num | ||
? str.slice(0, num >= ellipsisStr.length ? num - ellipsisStr.length : num) + | ||
ellipsisStr | ||
: str; | ||
``` | ||
|
||
```ts title="typescript" | ||
truncateString("boomerang", 7); // 'boom...' | ||
|
||
ellipsis("boomerang", 5, ".."); // "boo.." | ||
|
||
ellipsis("boomerang"); // "boomer..." | ||
|
||
ellipsis("boomerang", undefined, "♦♦♦"); // "boomer♦♦♦" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
title: equals [相等] | ||
tags: [object, array, type, advanced] | ||
author_title: Deepak Vishwakarma | ||
author_url: https://github.com/deepakshrma | ||
author_image_url: https://avatars2.githubusercontent.com/u/7682731?s=400 | ||
description: 在 TypeScript、JavaScript 和 Deno 中实现 "equals" 的方法。 | ||
image: https://www.positronx.io/wp-content/uploads/2018/11/positronx-banner-1152-1.jpg | ||
--- | ||
|
||
 | ||
 | ||
 | ||
|
||
执行两个值之间的深度比较,以确定它们是否等价。 | ||
|
||
检查这两个值是否相同,如果它们都是具有相同时间的 `Date` 对象,可以使用 `Date.getTime()`,或者如果它们都是非对象值并且具有等价的值(使用严格比较)。 | ||
|
||
检查只有一个值是 null 或 undefined,或者它们的原型不同。如果上述条件都不满足,使用 `Object.keys()` 检查这两个值是否具有相同数量的键,然后使用 Array.`prototype.every()` 检查第一个值中的每个键是否存在于第二个值中,并且通过递归调用此方法来检查它们是否等价。 | ||
|
||
```ts title="typescript" | ||
const equals = (a, b) => { | ||
if (a === b) return true; | ||
if (a instanceof Date && b instanceof Date) | ||
return a.getTime() === b.getTime(); | ||
if (!a || !b || (typeof a !== "object" && typeof b !== "object")) | ||
return a === b; | ||
if (a.prototype !== b.prototype) return false; | ||
let keys = Object.keys(a); | ||
if (keys.length !== Object.keys(b).length) return false; | ||
return keys.every((k) => equals(a[k], b[k])); | ||
}; | ||
``` | ||
|
||
```ts title="typescript" | ||
equals( | ||
{ a: [2, { e: 3 }], b: [4], c: "foo" }, | ||
{ a: [2, { e: 3 }], b: [4], c: "foo" } | ||
); // true | ||
equals([1, 2, 3], { 0: 1, 1: 2, 2: 3 }); // true | ||
|
||
// equals(1, "1");// compile error | ||
deepEquals(1, "1"); //false// no compile error | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.