Skip to content

Commit

Permalink
增加yeild和json-scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
tedjmzhang committed Jun 11, 2024
1 parent 045823b commit 89a7041
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 2 deletions.
39 changes: 39 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@metalsmith/layouts": "^2.7.0",
"@metalsmith/markdown": "^1.10.0",
"@metalsmith/permalinks": "^3.0.1",
"ajv": "^8.16.0",
"chalk": "^5.3.0",
"commander": "^12.1.0",
"gogocode": "^1.0.55",
Expand Down
15 changes: 15 additions & 0 deletions src/frontend/javascript/js/generator.js
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()
21 changes: 21 additions & 0 deletions src/frontend/javascript/js/yeild.js
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
27 changes: 27 additions & 0 deletions src/frontend/javascript/yeild.md
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
```
26 changes: 26 additions & 0 deletions src/frontend/json-scheme/index.js
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)
24 changes: 24 additions & 0 deletions src/frontend/json-scheme/index.md
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"
25 changes: 25 additions & 0 deletions src/frontend/library/typescript/decorator.ts
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() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"target": "ES5",
"module": "CommonJS",
"outDir": "out",
"sourceMap": true
"sourceMap": true,
"experimentalDecorators": true
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"target": "ES5",
"module": "CommonJS",
"outDir": "out",
"sourceMap": true
"sourceMap": true,
"experimentalDecorators": true
}
}

0 comments on commit 89a7041

Please sign in to comment.