@ibyar/platform 0.5.4
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/LICENSE +21 -0
- package/README.md +39 -0
- package/index.d.ts +1 -0
- package/index.js +2 -0
- package/index.js.map +1 -0
- package/package.json +41 -0
- package/patch/diff.d.ts +36 -0
- package/patch/diff.js +94 -0
- package/patch/diff.js.map +1 -0
- package/patch/index.d.ts +1 -0
- package/patch/index.js +2 -0
- package/patch/index.js.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020-2022 @ibyar/platform
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Aurora Expression
|
|
2
|
+
|
|
3
|
+
[![NPM Version][npm-image]][npm-url]
|
|
4
|
+
[![NPM Downloads][downloads-image]][downloads-url]
|
|
5
|
+
[![LICENSE][license-img]][license-url]
|
|
6
|
+
[![lerna][lerna-img]][lerna-url]
|
|
7
|
+
![GitHub contributors][contributors]
|
|
8
|
+
|
|
9
|
+
[npm-image]: https://img.shields.io/npm/v/@ibyar/platform.svg?logo=npm&logoColor=fff&label=NPM+package&color=limegreen
|
|
10
|
+
[npm-url]: https://npmjs.org/package/@ibyar/platform
|
|
11
|
+
[downloads-image]: https://img.shields.io/npm/dt/@ibyar/platform
|
|
12
|
+
[downloads-url]: https://npmjs.org/package/@ibyar/platform
|
|
13
|
+
[license-img]: https://img.shields.io/github/license/ibyar/aurora
|
|
14
|
+
[license-url]: https://github.com/ibyar/aurora/blob/master/LICENSE
|
|
15
|
+
[lerna-img]: https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg
|
|
16
|
+
[lerna-url]: https://lerna.js.org/
|
|
17
|
+
[contributors]: https://img.shields.io/github/contributors/ibyar/aurora
|
|
18
|
+
|
|
19
|
+
Aurora Platform, A utility package for Aurora.
|
|
20
|
+
|
|
21
|
+
- this package has no dependance, can work on both (Node.js) and any Web Browsers, that support modules and class syntax
|
|
22
|
+
|
|
23
|
+
## `Install`
|
|
24
|
+
|
|
25
|
+
``` bash
|
|
26
|
+
npm i --save @ibyar/platform
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
``` bash
|
|
30
|
+
yarn add @ibyar/platform
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
## Example
|
|
35
|
+
```ts
|
|
36
|
+
import { diff} from '@ibyar/platform';
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
```
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './patch/index.js';
|
package/index.js
ADDED
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/platform/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC","sourcesContent":["export * from './patch/index.js';"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ibyar/platform",
|
|
3
|
+
"version": "0.5.4",
|
|
4
|
+
"description": "Aurora Platform, A utility package for Aurora",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://github.com/ibyar/aurora/tree/dev/packages/platform",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/ibyar/aurora.git",
|
|
10
|
+
"directory": "packages/platform"
|
|
11
|
+
},
|
|
12
|
+
"main": "./index.js",
|
|
13
|
+
"module": "./index.js",
|
|
14
|
+
"types": "./index.d.ts",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": "./index.js"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"*"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"reflect-metadata": "^0.1.13",
|
|
27
|
+
"tslib": "^2.4.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"typescript": "^4.6.4"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc --build --verbose"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"aurora",
|
|
37
|
+
"ibyar",
|
|
38
|
+
"platform"
|
|
39
|
+
],
|
|
40
|
+
"gitHead": "0e62f914850836abc384d26c9f94a7b8389a2c0d"
|
|
41
|
+
}
|
package/patch/diff.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export declare type TrackBy<T, R = T> = (index: number, item: T) => R;
|
|
2
|
+
export declare type DiffOptions<T, R = T> = {
|
|
3
|
+
trackBy?: TrackBy<T, R>;
|
|
4
|
+
};
|
|
5
|
+
export declare enum PatchOperation {
|
|
6
|
+
ADD = "add",
|
|
7
|
+
REMOVE = "remove",
|
|
8
|
+
REPLACE = "replace",
|
|
9
|
+
MOVE = "move",
|
|
10
|
+
KEEP = "keep"
|
|
11
|
+
}
|
|
12
|
+
export declare type PatchArray<T> = {
|
|
13
|
+
currentIndex: number;
|
|
14
|
+
nextIndex: number;
|
|
15
|
+
op: PatchOperation;
|
|
16
|
+
item: T;
|
|
17
|
+
};
|
|
18
|
+
export declare type PatchObject = {
|
|
19
|
+
key: string;
|
|
20
|
+
op: PatchOperation;
|
|
21
|
+
};
|
|
22
|
+
export declare const PatchRoot: {
|
|
23
|
+
op: PatchOperation;
|
|
24
|
+
root: boolean;
|
|
25
|
+
};
|
|
26
|
+
export declare type DiffPatch<T> = PatchArray<T> | PatchObject;
|
|
27
|
+
export declare class JSONPatch {
|
|
28
|
+
protected diffArray<T, R = T>(input: T[], output: T[], options?: DiffOptions<T, R>): PatchArray<T>[] | [typeof PatchRoot];
|
|
29
|
+
protected diffObject<T = any>(input: T, output: T): PatchObject[] | [typeof PatchRoot];
|
|
30
|
+
diff<T = any>(input: T, output: T): PatchObject[] | [typeof PatchRoot];
|
|
31
|
+
diff<T>(input: T[], output: T[]): PatchArray<T>[] | [typeof PatchRoot];
|
|
32
|
+
diff<T, R>(input: T[], output: T[], options?: DiffOptions<T, R>): PatchArray<T>[] | [typeof PatchRoot];
|
|
33
|
+
}
|
|
34
|
+
export declare function diff<T = any>(input: T, output: T): PatchObject[] | [typeof PatchRoot];
|
|
35
|
+
export declare function diff<T>(input: T[], output: T[]): PatchArray<T>[] | [typeof PatchRoot];
|
|
36
|
+
export declare function diff<T, R>(input: T[], output: T[], options?: DiffOptions<T, R>): PatchArray<T>[] | [typeof PatchRoot];
|
package/patch/diff.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
export var PatchOperation;
|
|
2
|
+
(function (PatchOperation) {
|
|
3
|
+
PatchOperation["ADD"] = "add";
|
|
4
|
+
PatchOperation["REMOVE"] = "remove";
|
|
5
|
+
PatchOperation["REPLACE"] = "replace";
|
|
6
|
+
PatchOperation["MOVE"] = "move";
|
|
7
|
+
PatchOperation["KEEP"] = "keep";
|
|
8
|
+
})(PatchOperation || (PatchOperation = {}));
|
|
9
|
+
;
|
|
10
|
+
export const PatchRoot = { op: PatchOperation.REPLACE, root: true };
|
|
11
|
+
export class JSONPatch {
|
|
12
|
+
diffArray(input, output, options) {
|
|
13
|
+
const trackBy = options?.trackBy;
|
|
14
|
+
const identityInput = (trackBy && input.map((item, index) => trackBy(index, item))) ?? input;
|
|
15
|
+
const identityOutput = (trackBy && output.map((item, index) => trackBy(index, item))) ?? output;
|
|
16
|
+
const diffArray = [];
|
|
17
|
+
identityInput.forEach((item, index) => {
|
|
18
|
+
const nextIndex = identityOutput.indexOf(item);
|
|
19
|
+
const op = nextIndex == -1
|
|
20
|
+
? PatchOperation.REMOVE
|
|
21
|
+
: index == nextIndex
|
|
22
|
+
? PatchOperation.KEEP
|
|
23
|
+
: PatchOperation.MOVE;
|
|
24
|
+
diffArray.push({
|
|
25
|
+
currentIndex: index,
|
|
26
|
+
nextIndex: nextIndex,
|
|
27
|
+
op: op,
|
|
28
|
+
item: output[nextIndex]
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
identityOutput.forEach((item, index) => {
|
|
32
|
+
const oldIndex = identityInput.indexOf(item);
|
|
33
|
+
if (oldIndex == -1) {
|
|
34
|
+
diffArray.push({
|
|
35
|
+
currentIndex: -1,
|
|
36
|
+
nextIndex: index,
|
|
37
|
+
op: PatchOperation.ADD,
|
|
38
|
+
item: output[index]
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
const patchArray = new Array(output.length);
|
|
43
|
+
let i = 0;
|
|
44
|
+
diffArray.forEach(item => {
|
|
45
|
+
if (item.ignore) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
let patch = item;
|
|
49
|
+
if (PatchOperation.REMOVE == item.op) {
|
|
50
|
+
const replacedItem = diffArray.find(r => !r.ignore && r.nextIndex == item.currentIndex);
|
|
51
|
+
if (replacedItem) {
|
|
52
|
+
if (PatchOperation.MOVE == replacedItem.op) {
|
|
53
|
+
item.ignore = replacedItem.ignore = true;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
replacedItem.ignore = true;
|
|
58
|
+
patch = {
|
|
59
|
+
currentIndex: item.currentIndex,
|
|
60
|
+
nextIndex: replacedItem.nextIndex,
|
|
61
|
+
op: PatchOperation.REPLACE,
|
|
62
|
+
item: replacedItem.item
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
patchArray[i++] = patch;
|
|
68
|
+
});
|
|
69
|
+
const removed = patchArray
|
|
70
|
+
.filter(item => !item.ignore && PatchOperation.REMOVE == item.op)
|
|
71
|
+
.sort((a, b) => b.currentIndex - a.currentIndex);
|
|
72
|
+
const apply = patchArray
|
|
73
|
+
.filter(item => !item.ignore && PatchOperation.REMOVE != item.op)
|
|
74
|
+
.sort((a, b) => a.nextIndex - b.nextIndex);
|
|
75
|
+
return removed.concat(apply);
|
|
76
|
+
}
|
|
77
|
+
diffObject(input, output) {
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
diff(input, output, options) {
|
|
81
|
+
if (Array.isArray(input) && Array.isArray(output)) {
|
|
82
|
+
return this.diffArray(input, output, options);
|
|
83
|
+
}
|
|
84
|
+
else if (typeof input == 'object' && typeof output == 'object') {
|
|
85
|
+
return this.diffObject(input, output);
|
|
86
|
+
}
|
|
87
|
+
return [PatchRoot];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const jsonPatch = new JSONPatch();
|
|
91
|
+
export function diff(input, output, options) {
|
|
92
|
+
return jsonPatch.diff(input, output, options);
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=diff.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diff.js","sourceRoot":"","sources":["../../../src/platform/patch/diff.ts"],"names":[],"mappings":"AAGA,MAAM,CAAN,IAAY,cAMX;AAND,WAAY,cAAc;IACzB,6BAAW,CAAA;IACX,mCAAiB,CAAA;IACjB,qCAAmB,CAAA;IACnB,+BAAa,CAAA;IACb,+BAAa,CAAA;AACd,CAAC,EANW,cAAc,KAAd,cAAc,QAMzB;AAAA,CAAC;AAIF,MAAM,CAAC,MAAM,SAAS,GAAG,EAAE,EAAE,EAAE,cAAc,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;AAGpE,MAAM,OAAO,SAAS;IAEX,SAAS,CAAW,KAAU,EAAE,MAAW,EAAE,OAA2B;QAEjF,MAAM,OAAO,GAAG,OAAO,EAAE,OAAO,CAAC;QACjC,MAAM,aAAa,GAAG,CAAC,OAAO,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC;QAC7F,MAAM,cAAc,GAAG,CAAC,OAAO,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC;QAChG,MAAM,SAAS,GAAgB,EAAE,CAAC;QAClC,aAAa,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE;YACrC,MAAM,SAAS,GAAG,cAAc,CAAC,OAAO,CAAC,IAAa,CAAC,CAAC;YACxD,MAAM,EAAE,GAAmB,SAAS,IAAI,CAAC,CAAC;gBACzC,CAAC,CAAC,cAAc,CAAC,MAAM;gBACvB,CA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;YAClD,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;SAC9C;aAAM,IAAI,OAAO,KAAK,IAAI,QAAQ,IAAI,OAAO,MAAM,IAAI,QAAQ,EAAE;YACjE,OAAO,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;SACtC;QACD,OAAO,CAAC,SAAS,CAAC,CAAC;IACpB,CAAC;CACD;AAED,MAAM,SAAS,GAAG,IAAI,SAAS,EAAE,CAAC;AAKlC,MAAM,UAAU,IAAI,CAAC,KAAU,EAAE,MAAW,EAAE,OAA+B;IAC5E,OAAO,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;AAC/C,CAAC","sourcesContent":["export type TrackBy<T, R = T> = (index: number, item: T) => R;\nexport type DiffOptions<T, R = T> = { trackBy?: TrackBy<T, R> };\n\nexport enum PatchOperation {\n\tADD = 'add',\n\tREMOVE = 'remove',\n\tREPLACE = 'replace',\n\tMOVE = 'move',\n\tKEEP = 'keep'\n};\n\nexport type PatchArray<T> = { currentIndex: number, nextIndex: number, op: PatchOperation, item: T };\nexport type PatchObject = { key: string, op: PatchOperation };\nexport const PatchRoot = { op: PatchOperation.REPLACE, root: true };\nexport type DiffPatch<T> = PatchArray<T> | PatchObject;\n\nexport class JSONPatch {\n\n\tprotected diffArray<T, R = T>(input: T[], output: T[], options?: DiffOptions<T, R>): PatchArray<T>[] | [typeof PatchRoot] {\n\t\ttype DiffArray = PatchArray<T> & { ignore?: boolean };\n\t\tconst trackBy = options?.trackBy;\n\t\tconst identityInput = (trackBy && input.map((item, index) => trackBy(index, item))) ?? input;\n\t\tconst identityOutput = (trackBy && output.map((item, index) => trackBy(index, item))) ?? output;\n\t\tconst diffArray: DiffArray[] = [];\n\t\tidentityInput.forEach((item, index) => {\n\t\t\tconst nextIndex = identityOutput.indexOf(item as T & R);\n\t\t\tconst op: PatchOperation = nextIndex == -1\n\t\t\t\t? PatchOperation.REMOVE\n\t\t\t\t: index == nextIndex\n\t\t\t\t\t? PatchOperation.KEEP\n\t\t\t\t\t: PatchOperation.MOVE;\n\t\t\tdiffArray.push({\n\t\t\t\tcurrentIndex: index,\n\t\t\t\tnextIndex: nextIndex,\n\t\t\t\top: op,\n\t\t\t\titem: output[nextIndex]\n\t\t\t});\n\t\t});\n\t\tidentityOutput.forEach((item, index) => {\n\t\t\tconst oldIndex = identityInput.indexOf(item as T & R);\n\t\t\tif (oldIndex == -1) {\n\t\t\t\tdiffArray.push({\n\t\t\t\t\tcurrentIndex: -1,\n\t\t\t\t\tnextIndex: index,\n\t\t\t\t\top: PatchOperation.ADD,\n\t\t\t\t\titem: output[index]\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\t\tconst patchArray: DiffArray[] = new Array<DiffArray>(output.length);\n\t\tlet i = 0;\n\t\tdiffArray.forEach(item => {\n\t\t\tif (item.ignore) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tlet patch = item;\n\t\t\tif (PatchOperation.REMOVE == item.op) {\n\t\t\t\tconst replacedItem = diffArray.find(r => !r.ignore && r.nextIndex == item.currentIndex);\n\t\t\t\tif (replacedItem) {\n\t\t\t\t\tif (PatchOperation.MOVE == replacedItem.op) {\n\t\t\t\t\t\titem.ignore = replacedItem.ignore = true;\n\t\t\t\t\t\treturn;\n\t\t\t\t\t} else {\n\t\t\t\t\t\treplacedItem.ignore = true;\n\t\t\t\t\t\tpatch = {\n\t\t\t\t\t\t\tcurrentIndex: item.currentIndex,\n\t\t\t\t\t\t\tnextIndex: replacedItem.nextIndex,\n\t\t\t\t\t\t\top: PatchOperation.REPLACE,\n\t\t\t\t\t\t\titem: replacedItem.item\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tpatchArray[i++] = patch;\n\t\t});\n\t\tconst removed = patchArray\n\t\t\t.filter(item => !item.ignore && PatchOperation.REMOVE == item.op)\n\t\t\t.sort((a, b) => b.currentIndex - a.currentIndex);\n\t\tconst apply = patchArray\n\t\t\t.filter(item => !item.ignore && PatchOperation.REMOVE != item.op)\n\t\t\t.sort((a, b) => a.nextIndex - b.nextIndex);\n\t\treturn removed.concat(apply) as PatchArray<T>[];\n\t}\n\n\tprotected diffObject<T = any>(input: T, output: T): PatchObject[] | [typeof PatchRoot] {\n\t\treturn [];\n\t}\n\n\tdiff<T = any>(input: T, output: T): PatchObject[] | [typeof PatchRoot];\n\tdiff<T>(input: T[], output: T[]): PatchArray<T>[] | [typeof PatchRoot];\n\tdiff<T, R>(input: T[], output: T[], options?: DiffOptions<T, R>): PatchArray<T>[] | [typeof PatchRoot];\n\tdiff(input: any, output: any, options?: DiffOptions<any, any>): DiffPatch<any>[] | [typeof PatchRoot] {\n\t\tif (Array.isArray(input) && Array.isArray(output)) {\n\t\t\treturn this.diffArray(input, output, options);\n\t\t} else if (typeof input == 'object' && typeof output == 'object') {\n\t\t\treturn this.diffObject(input, output);\n\t\t}\n\t\treturn [PatchRoot];\n\t}\n}\n\nconst jsonPatch = new JSONPatch();\n\nexport function diff<T = any>(input: T, output: T): PatchObject[] | [typeof PatchRoot];\nexport function diff<T>(input: T[], output: T[]): PatchArray<T>[] | [typeof PatchRoot];\nexport function diff<T, R>(input: T[], output: T[], options?: DiffOptions<T, R>): PatchArray<T>[] | [typeof PatchRoot];\nexport function diff(input: any, output: any, options?: DiffOptions<any, any>): DiffPatch<any>[] | [typeof PatchRoot] {\n\treturn jsonPatch.diff(input, output, options);\n}\n"]}
|
package/patch/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './diff.js';
|
package/patch/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/platform/patch/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC","sourcesContent":["export * from './diff.js';\n"]}
|