-
Notifications
You must be signed in to change notification settings - Fork 0
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
tedjmzhang
committed
Jun 11, 2024
1 parent
045823b
commit 89a7041
Showing
10 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,15 @@ | ||
function getValue() { | ||
let values = [] | ||
let value1 = fetch('http://baidu.com').then(res => { | ||
values.push(res) | ||
yield res; | ||
}) | ||
.then(res => { | ||
console.log('res', res); | ||
}) | ||
.catch(err2 => { | ||
console.log('err2', err2) | ||
}) | ||
} | ||
|
||
getValue() |
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,21 @@ | ||
let outValue = 0, outStep = 0; | ||
function* counter(value) { | ||
let step; | ||
|
||
while (true) { | ||
outStep = step = yield value++; | ||
outValue = value | ||
if (step) { | ||
value += step; | ||
} | ||
} | ||
} | ||
|
||
const generatorFunc = counter(0); | ||
console.log(generatorFunc.next().value); // 0 | ||
console.log(generatorFunc.next().value); // 1 | ||
console.log(generatorFunc.next().value); // 2 | ||
console.log(generatorFunc.next().value); // 3 | ||
console.log(generatorFunc.next(10).value); // 14 | ||
console.log(generatorFunc.next().value); // 15 | ||
console.log(generatorFunc.next(10).value); // 26 |
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,27 @@ | ||
# yeild | ||
|
||
就是程序执行到yeild的时候,会执行yeild后面跟着的表达式,然后立即返回值{value: 表达式值, done: true | false}。 | ||
等到下次再调用next(param)时,param将会作为`yeild 表达式`的返回值,可以通过`variable = yeild 表达式`获取param值,然后继续往下执行; | ||
直接调用next(), `yeild 表达式`的返回值为undefined; | ||
|
||
``` javascript | ||
function* counter(value) { | ||
let step; | ||
|
||
while (true) { | ||
step = yield value++; // 每次都执行yeild后面的value++之后停止,下次调用next(param)的时候通过step获取param值。然后往下执行,碰到yeild又会暂停,没碰到yeild则done为true | ||
if (step) { | ||
value += step; | ||
} | ||
} | ||
} | ||
|
||
const generatorFunc = counter(0); // 这里并没有调用函数 | ||
console.log(generatorFunc.next().value); // 0 // 这里调用next之后才开始调用函数 | ||
console.log(generatorFunc.next().value); // 1 | ||
console.log(generatorFunc.next().value); // 2 | ||
console.log(generatorFunc.next().value); // 3 | ||
console.log(generatorFunc.next(10).value); // 14 | ||
console.log(generatorFunc.next().value); // 15 | ||
console.log(generatorFunc.next(10).value); // 26 | ||
``` |
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,26 @@ | ||
// Node.js require: | ||
const Ajv = require("ajv") | ||
|
||
const ajv = new Ajv() // options can be passed, e.g. {allErrors: true} | ||
|
||
const schema = { | ||
// $schema: "https://json-schema.org/draft/2020-12/schema", | ||
type: "object", | ||
properties: { | ||
foo: {type: "integer", "$comment": "Created by John Doe"}, | ||
bar: {type: "string"}, | ||
date1: {type: "string", pattern: "\\d*-\\d*-\\d*"} | ||
}, | ||
required: ["foo"], | ||
additionalProperties: false, | ||
} | ||
|
||
const data = { | ||
foo: 1, | ||
bar: "abc", | ||
date1: "2019-12-12" | ||
} | ||
|
||
const validate = ajv.compile(schema) | ||
const valid = validate(data) | ||
if (!valid) console.log(validate.errors) |
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,24 @@ | ||
# json-scheme json语义化语法 | ||
[官方介绍文档链接](https://json-schema.org/learn/getting-started-step-by-step) | ||
|
||
[实现库](https://json-schema.org/implementations#validators-javascript)有很多,比如ajv | ||
|
||
## 总结 | ||
1. 必须在type定义类型,以下为所有可用类型 | ||
string | ||
number | ||
integer | ||
object | ||
array | ||
boolean | ||
null | ||
|
||
2. 对于每一类型,都有对应的扩展属性。 | ||
比如{type: "string"},可以加上pattern或者format或者minLength来额外定义string。 | ||
{type: "number"},可以加Multiples来定义基础倍数 | ||
{type: "object"},可以加properties定义属性,以及additionalProperties表示是否可冗余属性 | ||
|
||
3. 通用属性 | ||
注释 $comment | ||
枚举 "enum": ["red", "amber", "green"] | ||
常量 "const": "United States of America" |
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 @@ | ||
function first(a, b, c) { | ||
console.log("first(): factory evaluated", a, b, c); | ||
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { | ||
console.log("first(): called"); | ||
}; | ||
} | ||
|
||
function second() { | ||
console.log("second(): factory evaluated"); | ||
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { | ||
console.log("second(): called"); | ||
}; | ||
} | ||
|
||
function sealed(constructor: Function) { | ||
Object.seal(constructor); | ||
Object.seal(constructor.prototype); | ||
} | ||
|
||
@sealed | ||
class ExampleClass { | ||
@first(1, 2,3) | ||
@second() | ||
methodFn1() {} | ||
} |
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