oipage 1.3.0-alpha.1 → 1.3.0-alpha.3

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.
@@ -0,0 +1,211 @@
1
+ /*!
2
+ * json of OIPage v1.3.0-alpha.3
3
+ * git+https://github.com/oi-contrib/OIPage.git
4
+ */
5
+ import {reader} from "../reader/index.js";
6
+ let calcValue = function (word) {
7
+ if (word.type != 'string' && word.type != 'object') {
8
+
9
+ // 数字
10
+ if (/[+-]{0,1}\d{1,}\.{0,1}\d{0,}/.test(word.value)) {
11
+ return +word.value;
12
+ }
13
+
14
+ // undefined
15
+ else if (word.value == 'undefined') {
16
+ return undefined;
17
+ }
18
+
19
+ // null
20
+ else if (word.value == 'null') {
21
+ return null;
22
+ }
23
+
24
+ // false
25
+ else if (word.value == 'false') {
26
+ return false;
27
+ }
28
+
29
+ // true
30
+ else if (word.value == 'true') {
31
+ return true;
32
+ }
33
+
34
+ }
35
+
36
+ return word.value;
37
+ };
38
+
39
+ let toValue = function (wordArray) {
40
+ let value;
41
+
42
+ // 是json
43
+ if (wordArray[0].value == '{') {
44
+ value = {};
45
+ for (let i = 3; i < wordArray.length; i += 4) {
46
+ value[wordArray[i - 2].value] = calcValue(wordArray[i]);
47
+ }
48
+ }
49
+
50
+ // 数组
51
+ else {
52
+ value = [];
53
+ for (let i = 2; i < wordArray.length; i += 2) {
54
+ value.push(calcValue(wordArray[i - 1]));
55
+ }
56
+ }
57
+
58
+ return {
59
+ type: "object",
60
+ value: value
61
+ };
62
+ };
63
+
64
+ let analyseWord = function (express) {
65
+ // 剔除开头和结尾的空白
66
+ express = express.trim();
67
+
68
+ // 获取字符串分析对象
69
+ let readerHandler = reader(express);
70
+
71
+ let wordArray = [];
72
+ let tempWord = "";
73
+ readerHandler.readNext();
74
+
75
+ // 定义一个追加普通串的方法
76
+ let pushNormal = function () {
77
+ tempWord = tempWord.trim();
78
+ if (tempWord != '') {
79
+ wordArray.push({
80
+ type: /\d%/.test(tempWord) ? "string" : "normal",
81
+ value: tempWord
82
+ });
83
+ }
84
+ tempWord = "";
85
+ };
86
+
87
+ while (true) {
88
+
89
+ if (readerHandler.index >= express.length) break;
90
+
91
+ // 单行注释
92
+ if (readerHandler.getNextN(2) == '//') {
93
+ while (!/\n/.test(readerHandler.readNext()) && readerHandler.index < express.length);
94
+ }
95
+
96
+ // 多行注释
97
+ else if (readerHandler.getNextN(2) == '/*') {
98
+ while (readerHandler.getNextN(2) != '*/') {
99
+ if (readerHandler.index >= express.length) {
100
+ throw new Error("Multiline comment not closed correctly : " + express + "\nstep='analyseWord-searchEndComment'");
101
+ }
102
+ readerHandler.readNext();
103
+ }
104
+ readerHandler.readNext();
105
+ readerHandler.readNext();
106
+ }
107
+
108
+ // 如果是边界符号
109
+ else if (['{', '}', ',', '[', ']', ':'].indexOf(readerHandler.value) > -1) {
110
+ pushNormal();
111
+
112
+ wordArray.push({
113
+ type: "insign",
114
+ value: readerHandler.value
115
+ });
116
+ readerHandler.readNext();
117
+ }
118
+
119
+ // 如果遇到字符串,应该是一个独立的单词
120
+ else if (['"', "'"].indexOf(readerHandler.value) > -1) {
121
+
122
+ let tempStrWord = "";
123
+ while (['"', "'"].indexOf(readerHandler.readNext()) < 0) {
124
+ if (readerHandler.index >= express.length) {
125
+ throw new Error("The string is not closed correctly : " + express + "\nstep='analyseWord-searchString',currentStrWord=" + tempStrWord);
126
+ }
127
+ tempStrWord += readerHandler.value;
128
+ }
129
+ readerHandler.readNext();
130
+ wordArray.push({
131
+ type: "string",
132
+ value: tempStrWord
133
+ });
134
+
135
+ } else {
136
+ tempWord += readerHandler.value;
137
+ readerHandler.readNext();
138
+ }
139
+
140
+ }
141
+
142
+ return wordArray;
143
+ };
144
+
145
+ export function strToJson(express) {
146
+ if (typeof express === "string") {
147
+
148
+ // 先分析出来单词
149
+ let wordArray = analyseWord(express);
150
+
151
+ /**
152
+ * 思路:
153
+ * 从后往前找,找到第一个需要归结的,直接归结,
154
+ * 归结完毕以后,继续,直到找到开头,说明归结完毕,
155
+ * 这样设计的好处是:
156
+ * 从后往前找,一定是叶子,这就消除了递归。
157
+ */
158
+ let i = wordArray.length - 1, j;
159
+
160
+ // 只要单词数组归结完毕
161
+ while (wordArray.length > 1) {
162
+
163
+ // 从后往前找第一个需要归结的子对象
164
+ while (i >= 0 && (wordArray[i].type != 'insign' || ['{', '['].indexOf(wordArray[i].value) < 0)) {
165
+ i = i - 1;
166
+ }
167
+
168
+ if (i < 0) {
169
+ // 如果到开头都没有遇到,缺少开始符号
170
+ throw new Error("Illegal express : " + express + "\nstep='toOne-searchBeginIndex',wordArray=" + wordArray);
171
+ }
172
+
173
+ // 然后合并
174
+ j = i + 1;
175
+ let subWordArray = [wordArray[i]];
176
+ while (j < wordArray.length && (wordArray[j].type != 'insign' || wordArray[j].value != {
177
+ "{": "}",
178
+ "[": "]"
179
+ }[wordArray[i].value])) {
180
+ subWordArray.push(wordArray[j]);
181
+ j = j + 1;
182
+ }
183
+
184
+ if (j >= wordArray.length) {
185
+ // 如果到结尾都没有需要应该闭合的符号,缺少闭合符号
186
+ throw new Error("Illegal express : " + express + "\nstep='toOne-searchEndIndex',wordArray=" + wordArray);
187
+ } else {
188
+
189
+ // 结尾追加进去
190
+ subWordArray.push(wordArray[j]);
191
+
192
+ // 归结
193
+ wordArray[i] = toValue(subWordArray);
194
+
195
+ // 调整
196
+ wordArray.splice(i + 1, j - i);
197
+ }
198
+
199
+
200
+ }
201
+
202
+ // 返回计算结果
203
+ return wordArray[0].value;
204
+
205
+ } else {
206
+
207
+ throw new Error('The data passed is not a string.');
208
+
209
+ }
210
+
211
+ }
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * onReady of OIPage v1.3.0-alpha.1
2
+ * onReady of OIPage v1.3.0-alpha.3
3
3
  * git+https://github.com/oi-contrib/OIPage.git
4
4
  */
5
5
 
@@ -0,0 +1,5 @@
1
+ export interface performChunkType {
2
+ (chunkFun: () => boolean): void
3
+ }
4
+
5
+ export let reader: performChunkType
@@ -0,0 +1,24 @@
1
+ /*!
2
+ * performChunk of OIPage v1.3.0-alpha.3
3
+ * git+https://github.com/oi-contrib/OIPage.git
4
+ */
5
+
6
+ export function performChunk(chunkFun) {
7
+ let isRuning = true;
8
+
9
+ function run() {
10
+ if (!isRuning) return;
11
+
12
+ // 被调度为一个任务队列中的回调函数,仅在浏览器空闲时执行
13
+ // https://developer.mozilla.org/zh-CN/docs/Web/API/Window/requestIdleCallback
14
+ requestIdleCallback(function (idle) {
15
+
16
+ // 如果当前剩余时间还有,且还有剩余任务
17
+ while (idle.timeRemaining() > 0 && isRuning) {
18
+ isRuning = chunkFun()
19
+ }
20
+ run();
21
+ });
22
+ }
23
+ run();
24
+ }
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * reader of OIPage v1.3.0-alpha.1
2
+ * reader of OIPage v1.3.0-alpha.3
3
3
  * git+https://github.com/oi-contrib/OIPage.git
4
4
  */
5
5
 
@@ -18,7 +18,7 @@ export function reader(plain) {
18
18
 
19
19
  // 获取往后num个值
20
20
  handler.getNextN = function (num) {
21
- return express.substring(handler.index, num + handler.index > plain.length ? plain.length : num + handler.index);
21
+ return plain.substring(handler.index, num + handler.index > plain.length ? plain.length : num + handler.index);
22
22
  };
23
23
 
24
24
  return handler;
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * style of OIPage v1.3.0-alpha.1
2
+ * style of OIPage v1.3.0-alpha.3
3
3
  * git+https://github.com/oi-contrib/OIPage.git
4
4
  */
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * throttle of OIPage v1.3.0-alpha.1
2
+ * throttle of OIPage v1.3.0-alpha.3
3
3
  * git+https://github.com/oi-contrib/OIPage.git
4
4
  */
5
5