@php-wasm/scopes 0.1.0 → 0.1.2
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/.eslintrc.json +18 -0
- package/README.md +7 -0
- package/package.json +21 -21
- package/project.json +48 -0
- package/src/index.spec.ts +58 -0
- package/{index.d.ts → src/index.ts} +40 -4
- package/tsconfig.json +22 -0
- package/tsconfig.lib.json +10 -0
- package/tsconfig.spec.json +19 -0
- package/vite.config.ts +47 -0
- package/index.js +0 -32
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": ["../../../.eslintrc.json"],
|
|
3
|
+
"ignorePatterns": ["!**/*"],
|
|
4
|
+
"overrides": [
|
|
5
|
+
{
|
|
6
|
+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
|
|
7
|
+
"rules": {}
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"files": ["*.ts", "*.tsx"],
|
|
11
|
+
"rules": {}
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"files": ["*.js", "*.jsx"],
|
|
15
|
+
"rules": {}
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
}
|
package/README.md
ADDED
package/package.json
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
2
|
+
"name": "@php-wasm/scopes",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "PHP.wasm – scoped URLs utils",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/WordPress/wordpress-playground"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://developer.wordpress.org/playground",
|
|
10
|
+
"author": "The WordPress contributors",
|
|
11
|
+
"contributors": [
|
|
12
|
+
{
|
|
13
|
+
"name": "Adam Zielinski",
|
|
14
|
+
"email": "adam@adamziel.com",
|
|
15
|
+
"url": "https://github.com/adamziel"
|
|
16
|
+
}
|
|
17
|
+
],
|
|
18
|
+
"license": "GPL-2.0-or-later",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "index.js",
|
|
21
|
+
"types": "index.d.ts",
|
|
22
|
+
"gitHead": "a0eeb66fd5386bb56715fca2b89e9669a5b8618b"
|
|
23
23
|
}
|
package/project.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "php-wasm-scopes",
|
|
3
|
+
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
+
"sourceRoot": "packages/php-wasm/scopes/src",
|
|
5
|
+
"projectType": "library",
|
|
6
|
+
"targets": {
|
|
7
|
+
"build": {
|
|
8
|
+
"executor": "@wp-playground/nx-extensions:package-json",
|
|
9
|
+
"options": {
|
|
10
|
+
"tsConfig": "packages/php-wasm/scopes/tsconfig.lib.json",
|
|
11
|
+
"outputPath": "dist/packages/php-wasm/scopes",
|
|
12
|
+
"buildTarget": "php-wasm-scopes:build:bundle:production"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"build:bundle": {
|
|
16
|
+
"executor": "@nrwl/vite:build",
|
|
17
|
+
"outputs": ["{options.outputPath}"],
|
|
18
|
+
"options": {
|
|
19
|
+
"outputPath": "dist/packages/php-wasm/scopes"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"lint": {
|
|
23
|
+
"executor": "@nrwl/linter:eslint",
|
|
24
|
+
"outputs": ["{options.outputFile}"],
|
|
25
|
+
"options": {
|
|
26
|
+
"lintFilePatterns": ["packages/php-wasm/scopes/**/*.ts"]
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"test": {
|
|
30
|
+
"executor": "@nrwl/vite:test",
|
|
31
|
+
"outputs": ["coverage/packages/php-wasm/scopes"],
|
|
32
|
+
"options": {
|
|
33
|
+
"passWithNoTests": true,
|
|
34
|
+
"reportsDirectory": "../../../coverage/packages/php-wasm/scopes"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"typecheck": {
|
|
38
|
+
"executor": "@nrwl/workspace:run-commands",
|
|
39
|
+
"options": {
|
|
40
|
+
"commands": [
|
|
41
|
+
"yarn tsc -p packages/php-wasm/scopes/tsconfig.lib.json --noEmit",
|
|
42
|
+
"yarn tsc -p packages/php-wasm/scopes/tsconfig.spec.json --noEmit"
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"tags": []
|
|
48
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { getURLScope, isURLScoped, removeURLScope, setURLScope } from './index';
|
|
2
|
+
|
|
3
|
+
describe('getURLScope', () => {
|
|
4
|
+
it('should return the scope from a scoped URL', () => {
|
|
5
|
+
const url = new URL('http://localhost/scope:12345/index.php');
|
|
6
|
+
expect(getURLScope(url)).toBe('12345');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('should return null from a non-scoped URL', () => {
|
|
10
|
+
const url = new URL('http://localhost/index.php');
|
|
11
|
+
expect(getURLScope(url)).toBeNull();
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe('isURLScoped', () => {
|
|
16
|
+
it('should return true for a scoped URL', () => {
|
|
17
|
+
const url = new URL('http://localhost/scope:12345/index.php');
|
|
18
|
+
expect(isURLScoped(url)).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should return false for a non-scoped URL', () => {
|
|
22
|
+
const url = new URL('http://localhost/index.php');
|
|
23
|
+
expect(isURLScoped(url)).toBe(false);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('removeURLScope', () => {
|
|
28
|
+
it('should remove the scope from a scoped URL', () => {
|
|
29
|
+
const url = new URL('http://localhost/scope:12345/index.php');
|
|
30
|
+
expect(removeURLScope(url).href).toBe('http://localhost/index.php');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should return the same URL for a non-scoped URL', () => {
|
|
34
|
+
const url = new URL('http://localhost/index.php');
|
|
35
|
+
expect(removeURLScope(url)).toBe(url);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('setURLScope', () => {
|
|
40
|
+
it('should add the scope to a non-scoped URL', () => {
|
|
41
|
+
const url = new URL('http://localhost/index.php');
|
|
42
|
+
expect(setURLScope(url, '12345').href).toBe(
|
|
43
|
+
'http://localhost/scope:12345/index.php'
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should replace the scope in a scoped URL', () => {
|
|
48
|
+
const url = new URL('http://localhost/scope:12345/index.php');
|
|
49
|
+
expect(setURLScope(url, '67890').href).toBe(
|
|
50
|
+
'http://localhost/scope:67890/index.php'
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should remove the scope from a URL when the scope is null', () => {
|
|
55
|
+
const url = new URL('http://localhost/scope:12345/index.php');
|
|
56
|
+
expect(setURLScope(url, null).href).toBe('http://localhost/index.php');
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
*
|
|
12
12
|
* For more information, see the README section on scopes.
|
|
13
13
|
*/
|
|
14
|
+
|
|
14
15
|
/**
|
|
15
16
|
* Checks if the given URL contains scope information.
|
|
16
17
|
*
|
|
@@ -26,7 +27,10 @@
|
|
|
26
27
|
* @param url The URL to check.
|
|
27
28
|
* @returns `true` if the URL contains scope information, `false` otherwise.
|
|
28
29
|
*/
|
|
29
|
-
export
|
|
30
|
+
export function isURLScoped(url: URL): boolean {
|
|
31
|
+
return url.pathname.startsWith(`/scope:`);
|
|
32
|
+
}
|
|
33
|
+
|
|
30
34
|
/**
|
|
31
35
|
* Returns the scope stored in the given URL.
|
|
32
36
|
*
|
|
@@ -42,7 +46,13 @@ export declare function isURLScoped(url: URL): boolean;
|
|
|
42
46
|
* @param url The URL.
|
|
43
47
|
* @returns The scope if the URL contains a scope, `null` otherwise.
|
|
44
48
|
*/
|
|
45
|
-
export
|
|
49
|
+
export function getURLScope(url: URL): string | null {
|
|
50
|
+
if (isURLScoped(url)) {
|
|
51
|
+
return url.pathname.split('/')[1].split(':')[1];
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
46
56
|
/**
|
|
47
57
|
* Returns a new URL with the requested scope information.
|
|
48
58
|
*
|
|
@@ -62,7 +72,25 @@ export declare function getURLScope(url: URL): string | null;
|
|
|
62
72
|
* @param scope The scope value.
|
|
63
73
|
* @returns A new URL with the scope information in it.
|
|
64
74
|
*/
|
|
65
|
-
export
|
|
75
|
+
export function setURLScope(url: URL | string, scope: string | null): URL {
|
|
76
|
+
let newUrl = new URL(url);
|
|
77
|
+
|
|
78
|
+
if (isURLScoped(newUrl)) {
|
|
79
|
+
if (scope) {
|
|
80
|
+
const parts = newUrl.pathname.split('/');
|
|
81
|
+
parts[1] = `scope:${scope}`;
|
|
82
|
+
newUrl.pathname = parts.join('/');
|
|
83
|
+
} else {
|
|
84
|
+
newUrl = removeURLScope(newUrl);
|
|
85
|
+
}
|
|
86
|
+
} else if (scope) {
|
|
87
|
+
const suffix = newUrl.pathname === '/' ? '' : newUrl.pathname;
|
|
88
|
+
newUrl.pathname = `/scope:${scope}${suffix}`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return newUrl;
|
|
92
|
+
}
|
|
93
|
+
|
|
66
94
|
/**
|
|
67
95
|
* Returns a new URL without any scope information.
|
|
68
96
|
*
|
|
@@ -78,4 +106,12 @@ export declare function setURLScope(url: URL | string, scope: string | null): UR
|
|
|
78
106
|
* @param url The URL to remove scope information from.
|
|
79
107
|
* @returns A new URL without the scope information.
|
|
80
108
|
*/
|
|
81
|
-
export
|
|
109
|
+
export function removeURLScope(url: URL): URL {
|
|
110
|
+
if (!isURLScoped(url)) {
|
|
111
|
+
return url;
|
|
112
|
+
}
|
|
113
|
+
const newUrl = new URL(url);
|
|
114
|
+
const parts = newUrl.pathname.split('/');
|
|
115
|
+
newUrl.pathname = '/' + parts.slice(2).join('/');
|
|
116
|
+
return newUrl;
|
|
117
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"forceConsistentCasingInFileNames": true,
|
|
5
|
+
"strict": true,
|
|
6
|
+
"noImplicitOverride": true,
|
|
7
|
+
"noPropertyAccessFromIndexSignature": true,
|
|
8
|
+
"noImplicitReturns": true,
|
|
9
|
+
"noFallthroughCasesInSwitch": true,
|
|
10
|
+
"types": ["vitest"]
|
|
11
|
+
},
|
|
12
|
+
"files": [],
|
|
13
|
+
"include": [],
|
|
14
|
+
"references": [
|
|
15
|
+
{
|
|
16
|
+
"path": "./tsconfig.lib.json"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"path": "./tsconfig.spec.json"
|
|
20
|
+
}
|
|
21
|
+
]
|
|
22
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "../../dist/out-tsc",
|
|
5
|
+
"types": ["vitest/globals", "vitest/importMeta", "vite/client", "node"]
|
|
6
|
+
},
|
|
7
|
+
"include": [
|
|
8
|
+
"vite.config.ts",
|
|
9
|
+
"src/**/*.test.ts",
|
|
10
|
+
"src/**/*.spec.ts",
|
|
11
|
+
"src/**/*.test.tsx",
|
|
12
|
+
"src/**/*.spec.tsx",
|
|
13
|
+
"src/**/*.test.js",
|
|
14
|
+
"src/**/*.spec.js",
|
|
15
|
+
"src/**/*.test.jsx",
|
|
16
|
+
"src/**/*.spec.jsx",
|
|
17
|
+
"src/**/*.d.ts"
|
|
18
|
+
]
|
|
19
|
+
}
|
package/vite.config.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/// <reference types="vitest" />
|
|
2
|
+
import { defineConfig } from 'vite';
|
|
3
|
+
|
|
4
|
+
import viteTsConfigPaths from 'vite-tsconfig-paths';
|
|
5
|
+
import dts from 'vite-plugin-dts';
|
|
6
|
+
import { join } from 'path';
|
|
7
|
+
|
|
8
|
+
export default defineConfig({
|
|
9
|
+
cacheDir: '../../../node_modules/.vite/php-wasm-scope',
|
|
10
|
+
|
|
11
|
+
plugins: [
|
|
12
|
+
dts({
|
|
13
|
+
entryRoot: 'src',
|
|
14
|
+
tsConfigFilePath: join(__dirname, 'tsconfig.lib.json'),
|
|
15
|
+
skipDiagnostics: true,
|
|
16
|
+
}),
|
|
17
|
+
|
|
18
|
+
viteTsConfigPaths({
|
|
19
|
+
root: '../../../',
|
|
20
|
+
}),
|
|
21
|
+
],
|
|
22
|
+
|
|
23
|
+
// Configuration for building your library.
|
|
24
|
+
// See: https://vitejs.dev/guide/build.html#library-mode
|
|
25
|
+
build: {
|
|
26
|
+
lib: {
|
|
27
|
+
// Could also be a dictionary or array of multiple entry points.
|
|
28
|
+
entry: 'src/index.ts',
|
|
29
|
+
name: 'php-wasm-scope',
|
|
30
|
+
fileName: 'index',
|
|
31
|
+
formats: ['es'],
|
|
32
|
+
},
|
|
33
|
+
rollupOptions: {
|
|
34
|
+
// External packages that should not be bundled into your library.
|
|
35
|
+
external: [],
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
test: {
|
|
40
|
+
globals: true,
|
|
41
|
+
cache: {
|
|
42
|
+
dir: '../../../node_modules/.vitest',
|
|
43
|
+
},
|
|
44
|
+
environment: 'jsdom',
|
|
45
|
+
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
|
|
46
|
+
},
|
|
47
|
+
});
|
package/index.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
function p(e) {
|
|
2
|
-
return e.pathname.startsWith("/scope:");
|
|
3
|
-
}
|
|
4
|
-
function s(e) {
|
|
5
|
-
return p(e) ? e.pathname.split("/")[1].split(":")[1] : null;
|
|
6
|
-
}
|
|
7
|
-
function o(e, n) {
|
|
8
|
-
let t = new URL(e);
|
|
9
|
-
if (p(t))
|
|
10
|
-
if (n) {
|
|
11
|
-
const a = t.pathname.split("/");
|
|
12
|
-
a[1] = `scope:${n}`, t.pathname = a.join("/");
|
|
13
|
-
} else
|
|
14
|
-
t = i(t);
|
|
15
|
-
else if (n) {
|
|
16
|
-
const a = t.pathname === "/" ? "" : t.pathname;
|
|
17
|
-
t.pathname = `/scope:${n}${a}`;
|
|
18
|
-
}
|
|
19
|
-
return t;
|
|
20
|
-
}
|
|
21
|
-
function i(e) {
|
|
22
|
-
if (!p(e))
|
|
23
|
-
return e;
|
|
24
|
-
const n = new URL(e), t = n.pathname.split("/");
|
|
25
|
-
return n.pathname = "/" + t.slice(2).join("/"), n;
|
|
26
|
-
}
|
|
27
|
-
export {
|
|
28
|
-
s as getURLScope,
|
|
29
|
-
p as isURLScoped,
|
|
30
|
-
i as removeURLScope,
|
|
31
|
-
o as setURLScope
|
|
32
|
-
};
|