@wtasnorg/node-lib 0.0.3 → 0.0.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/.github/workflows/npm-test-on-pr.yml +30 -0
- package/README.md +5 -0
- package/docs/README.md +5 -0
- package/docs/docs.json +11 -3
- package/docs/functions/hello.md +1 -1
- package/package.json +2 -2
- package/src/pojo.d.ts +12 -0
- package/src/pojo.js +14 -0
- package/src/pojo.test.d.ts +2 -0
- package/src/pojo.test.js +58 -0
- package/src/pojo.test.ts +69 -0
- package/src/pojo.ts +18 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
name: Run tests on pull request
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
pull_request:
|
|
5
|
+
types: [opened, reopened, synchronize]
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: read
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
run-tests:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
|
|
14
|
+
steps:
|
|
15
|
+
- name: Checkout repository
|
|
16
|
+
uses: actions/checkout@v4
|
|
17
|
+
with:
|
|
18
|
+
fetch-depth: 0
|
|
19
|
+
|
|
20
|
+
- name: Setup Node.js
|
|
21
|
+
uses: actions/setup-node@v4
|
|
22
|
+
with:
|
|
23
|
+
node-version: 20
|
|
24
|
+
cache: npm
|
|
25
|
+
|
|
26
|
+
- name: Install dependencies
|
|
27
|
+
run: npm ci
|
|
28
|
+
|
|
29
|
+
- name: Run tests
|
|
30
|
+
run: npm test
|
package/README.md
CHANGED
|
@@ -5,6 +5,11 @@ A library project for nodejs. #nodejs #typescript #library
|
|
|
5
5
|
- [npm org](https://www.npmjs.com/org/wtasnorg)
|
|
6
6
|
- [github repo](https://github.com/wtasg/node-lib)
|
|
7
7
|
|
|
8
|
+
## Functions
|
|
9
|
+
|
|
10
|
+
1. hello (for debugging)
|
|
11
|
+
2. `pojo` for converting class objects to Plain Old Javascript Objects.
|
|
12
|
+
|
|
8
13
|
## Develop
|
|
9
14
|
|
|
10
15
|
```bash
|
package/docs/README.md
CHANGED
|
@@ -9,6 +9,11 @@ A library project for nodejs. #nodejs #typescript #library
|
|
|
9
9
|
- [npm org](https://www.npmjs.com/org/wtasnorg)
|
|
10
10
|
- [github repo](https://github.com/wtasg/node-lib)
|
|
11
11
|
|
|
12
|
+
## Functions
|
|
13
|
+
|
|
14
|
+
1. hello (for debugging)
|
|
15
|
+
2. `pojo` for converting class objects to Plain Old Javascript Objects.
|
|
16
|
+
|
|
12
17
|
## Develop
|
|
13
18
|
|
|
14
19
|
```bash
|
package/docs/docs.json
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"fileName": "hello.ts",
|
|
18
18
|
"line": 6,
|
|
19
19
|
"character": 15,
|
|
20
|
-
"url": "https://github.com/wtasg/node-lib/blob/
|
|
20
|
+
"url": "https://github.com/wtasg/node-lib/blob/be4189222f32cc9b2fdf81c380cde5c51f69046c/src/hello.ts#L6"
|
|
21
21
|
}
|
|
22
22
|
],
|
|
23
23
|
"signatures": [
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"fileName": "hello.ts",
|
|
52
52
|
"line": 6,
|
|
53
53
|
"character": 15,
|
|
54
|
-
"url": "https://github.com/wtasg/node-lib/blob/
|
|
54
|
+
"url": "https://github.com/wtasg/node-lib/blob/be4189222f32cc9b2fdf81c380cde5c51f69046c/src/hello.ts#L6"
|
|
55
55
|
}
|
|
56
56
|
],
|
|
57
57
|
"type": {
|
|
@@ -86,7 +86,15 @@
|
|
|
86
86
|
"readme": [
|
|
87
87
|
{
|
|
88
88
|
"kind": "text",
|
|
89
|
-
"text": "# @wtasnorg/node-lib\n\nA library project for nodejs. #nodejs #typescript #library\n\n- [npm org](https://www.npmjs.com/org/wtasnorg)\n- [github repo](https://github.com/wtasg/node-lib)\n\n##
|
|
89
|
+
"text": "# @wtasnorg/node-lib\n\nA library project for nodejs. #nodejs #typescript #library\n\n- [npm org](https://www.npmjs.com/org/wtasnorg)\n- [github repo](https://github.com/wtasg/node-lib)\n\n## Functions\n\n1. hello (for debugging)\n2. "
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
"kind": "code",
|
|
93
|
+
"text": "`pojo`"
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
"kind": "text",
|
|
97
|
+
"text": " for converting class objects to Plain Old Javascript Objects.\n\n## Develop\n\n"
|
|
90
98
|
},
|
|
91
99
|
{
|
|
92
100
|
"kind": "code",
|
package/docs/functions/hello.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
> **hello**(): `Promise`\<`string`\>
|
|
10
10
|
|
|
11
|
-
Defined in: [hello.ts:6](https://github.com/wtasg/node-lib/blob/
|
|
11
|
+
Defined in: [hello.ts:6](https://github.com/wtasg/node-lib/blob/be4189222f32cc9b2fdf81c380cde5c51f69046c/src/hello.ts#L6)
|
|
12
12
|
|
|
13
13
|
A sample function that should work to test if lib is installed correctly.
|
|
14
14
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wtasnorg/node-lib",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "node library",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"docs": "./node_modules/.bin/typedoc",
|
|
9
9
|
"docs:json": "./node_modules/.bin/typedoc --json docs/docs.json",
|
|
10
10
|
"docs:watch": "./node_modules/.bin/typedoc --watch",
|
|
11
|
-
"test": "node --test"
|
|
11
|
+
"test": "bash -c 'node --test src/**/*.test.js'"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
14
14
|
"library"
|
package/src/pojo.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a class instance into a plain JavaScript object.
|
|
3
|
+
* Copies only the instance's own enumerable data properties
|
|
4
|
+
* and excludes methods or prototype values.
|
|
5
|
+
*
|
|
6
|
+
* @template T
|
|
7
|
+
* @param {T} instance - A class instance to convert.
|
|
8
|
+
* @returns {Object<string, any>} A plain JavaScript object containing only data fields.
|
|
9
|
+
*/
|
|
10
|
+
declare function pojo<T extends object>(instance: T): Record<string, any>;
|
|
11
|
+
export { pojo };
|
|
12
|
+
//# sourceMappingURL=pojo.d.ts.map
|
package/src/pojo.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a class instance into a plain JavaScript object.
|
|
3
|
+
* Copies only the instance's own enumerable data properties
|
|
4
|
+
* and excludes methods or prototype values.
|
|
5
|
+
*
|
|
6
|
+
* @template T
|
|
7
|
+
* @param {T} instance - A class instance to convert.
|
|
8
|
+
* @returns {Object<string, any>} A plain JavaScript object containing only data fields.
|
|
9
|
+
*/
|
|
10
|
+
function pojo(instance) {
|
|
11
|
+
return Object.fromEntries(Object.entries(instance).filter(([_, value]) => typeof value !== "function"));
|
|
12
|
+
}
|
|
13
|
+
export { pojo };
|
|
14
|
+
//# sourceMappingURL=pojo.js.map
|
package/src/pojo.test.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { pojo } from "./pojo.js";
|
|
4
|
+
class User {
|
|
5
|
+
id;
|
|
6
|
+
name;
|
|
7
|
+
constructor(id, name) {
|
|
8
|
+
this.id = id;
|
|
9
|
+
this.name = name;
|
|
10
|
+
}
|
|
11
|
+
greet() {
|
|
12
|
+
return `hello ${this.name}`;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
test("pojo() should return only enumerable data fields", () => {
|
|
16
|
+
const u = new User(1, "Alice");
|
|
17
|
+
const out = pojo(u);
|
|
18
|
+
assert.deepEqual(out, {
|
|
19
|
+
id: 1,
|
|
20
|
+
name: "Alice"
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
test("pojo() should exclude methods", () => {
|
|
24
|
+
const u = new User(2, "Bob");
|
|
25
|
+
const out = pojo(u);
|
|
26
|
+
assert.ok(!("greet" in out));
|
|
27
|
+
});
|
|
28
|
+
test("pojo() should exclude prototype properties", () => {
|
|
29
|
+
class Product {
|
|
30
|
+
id;
|
|
31
|
+
constructor(id) {
|
|
32
|
+
this.id = id;
|
|
33
|
+
}
|
|
34
|
+
get info() {
|
|
35
|
+
return `Product:${this.id}`;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const p = new Product(101);
|
|
39
|
+
const out = pojo(p);
|
|
40
|
+
assert.deepEqual(out, { id: 101 });
|
|
41
|
+
assert.ok(!("info" in out));
|
|
42
|
+
});
|
|
43
|
+
test("pojo() should ignore non-enumerable own properties", () => {
|
|
44
|
+
class SecretBox {
|
|
45
|
+
visible = "open";
|
|
46
|
+
constructor() {
|
|
47
|
+
Object.defineProperty(this, "hidden", {
|
|
48
|
+
value: "secret",
|
|
49
|
+
enumerable: false
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const box = new SecretBox();
|
|
54
|
+
const out = pojo(box);
|
|
55
|
+
assert.deepEqual(out, { visible: "open" });
|
|
56
|
+
assert.ok(!("hidden" in out));
|
|
57
|
+
});
|
|
58
|
+
//# sourceMappingURL=pojo.test.js.map
|
package/src/pojo.test.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { pojo } from "./pojo.js";
|
|
4
|
+
|
|
5
|
+
class User {
|
|
6
|
+
id: number;
|
|
7
|
+
name: string;
|
|
8
|
+
|
|
9
|
+
constructor(id: number, name: string) {
|
|
10
|
+
this.id = id;
|
|
11
|
+
this.name = name;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
greet() {
|
|
15
|
+
return `hello ${this.name}`;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
test("pojo() should return only enumerable data fields", () => {
|
|
20
|
+
const u = new User(1, "Alice");
|
|
21
|
+
const out = pojo(u);
|
|
22
|
+
|
|
23
|
+
assert.deepEqual(out, {
|
|
24
|
+
id: 1,
|
|
25
|
+
name: "Alice"
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("pojo() should exclude methods", () => {
|
|
30
|
+
const u = new User(2, "Bob");
|
|
31
|
+
const out = pojo(u);
|
|
32
|
+
|
|
33
|
+
assert.ok(!("greet" in out));
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("pojo() should exclude prototype properties", () => {
|
|
37
|
+
class Product {
|
|
38
|
+
constructor(public id: number) { }
|
|
39
|
+
|
|
40
|
+
get info() {
|
|
41
|
+
return `Product:${this.id}`;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const p = new Product(101);
|
|
46
|
+
const out = pojo(p);
|
|
47
|
+
|
|
48
|
+
assert.deepEqual(out, { id: 101 });
|
|
49
|
+
assert.ok(!("info" in out));
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("pojo() should ignore non-enumerable own properties", () => {
|
|
53
|
+
class SecretBox {
|
|
54
|
+
visible = "open";
|
|
55
|
+
|
|
56
|
+
constructor() {
|
|
57
|
+
Object.defineProperty(this, "hidden", {
|
|
58
|
+
value: "secret",
|
|
59
|
+
enumerable: false
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const box = new SecretBox();
|
|
65
|
+
const out = pojo(box);
|
|
66
|
+
|
|
67
|
+
assert.deepEqual(out, { visible: "open" });
|
|
68
|
+
assert.ok(!("hidden" in out));
|
|
69
|
+
});
|
package/src/pojo.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a class instance into a plain JavaScript object.
|
|
3
|
+
* Copies only the instance's own enumerable data properties
|
|
4
|
+
* and excludes methods or prototype values.
|
|
5
|
+
*
|
|
6
|
+
* @template T
|
|
7
|
+
* @param {T} instance - A class instance to convert.
|
|
8
|
+
* @returns {Object<string, any>} A plain JavaScript object containing only data fields.
|
|
9
|
+
*/
|
|
10
|
+
function pojo<T extends object>(instance: T): Record<string, any> {
|
|
11
|
+
return Object.fromEntries(
|
|
12
|
+
Object.entries(instance).filter(([_, value]) => typeof value !== "function")
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
pojo
|
|
18
|
+
};
|