@sneat/data 0.1.1
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/eslint.config.js +7 -0
- package/ng-package.json +7 -0
- package/package.json +14 -0
- package/project.json +38 -0
- package/src/index.ts +2 -0
- package/src/lib/modified.ts +4 -0
- package/src/lib/record.spec.ts +26 -0
- package/src/lib/record.ts +28 -0
- package/src/test-setup.ts +3 -0
- package/tsconfig.json +13 -0
- package/tsconfig.lib.json +19 -0
- package/tsconfig.lib.prod.json +7 -0
- package/tsconfig.spec.json +31 -0
- package/vite.config.mts +10 -0
package/eslint.config.js
ADDED
package/ng-package.json
ADDED
package/package.json
ADDED
package/project.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "data",
|
|
3
|
+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
+
"projectType": "library",
|
|
5
|
+
"sourceRoot": "libs/data/src",
|
|
6
|
+
"prefix": "sneat",
|
|
7
|
+
"targets": {
|
|
8
|
+
"build": {
|
|
9
|
+
"executor": "@nx/angular:ng-packagr-lite",
|
|
10
|
+
"outputs": [
|
|
11
|
+
"{workspaceRoot}/dist/libs/data"
|
|
12
|
+
],
|
|
13
|
+
"options": {
|
|
14
|
+
"project": "libs/data/ng-package.json",
|
|
15
|
+
"tsConfig": "libs/data/tsconfig.lib.json"
|
|
16
|
+
},
|
|
17
|
+
"configurations": {
|
|
18
|
+
"production": {
|
|
19
|
+
"tsConfig": "libs/data/tsconfig.lib.prod.json"
|
|
20
|
+
},
|
|
21
|
+
"development": {}
|
|
22
|
+
},
|
|
23
|
+
"defaultConfiguration": "production"
|
|
24
|
+
},
|
|
25
|
+
"test": {
|
|
26
|
+
"executor": "@nx/vitest:test",
|
|
27
|
+
"outputs": [
|
|
28
|
+
"{workspaceRoot}/coverage/libs/data"
|
|
29
|
+
],
|
|
30
|
+
"options": {
|
|
31
|
+
"tsConfig": "libs/data/tsconfig.spec.json"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"lint": {
|
|
35
|
+
"executor": "@nx/eslint:lint"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { firstValueFrom, of } from 'rxjs';
|
|
2
|
+
import { mapToRecord, MightHaveId, IRecord } from './record';
|
|
3
|
+
|
|
4
|
+
describe('record', () => {
|
|
5
|
+
describe('mapToRecord', () => {
|
|
6
|
+
it('should map a DTO to a record with default state', async () => {
|
|
7
|
+
const dto: MightHaveId = { id: 'test-id' };
|
|
8
|
+
const record: IRecord<MightHaveId> = await firstValueFrom(
|
|
9
|
+
of(dto).pipe(mapToRecord()),
|
|
10
|
+
);
|
|
11
|
+
expect(record.id).toBe('test-id');
|
|
12
|
+
expect(record.dbo).toBe(dto);
|
|
13
|
+
expect(record.state).toBe('unchanged');
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('should map a DTO to a record with specified state', async () => {
|
|
17
|
+
const dto: MightHaveId = { id: 'test-id' };
|
|
18
|
+
const record: IRecord<MightHaveId> = await firstValueFrom(
|
|
19
|
+
of(dto).pipe(mapToRecord('loading')),
|
|
20
|
+
);
|
|
21
|
+
expect(record.id).toBe('test-id');
|
|
22
|
+
expect(record.dbo).toBe(dto);
|
|
23
|
+
expect(record.state).toBe('loading');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { OperatorFunction } from 'rxjs';
|
|
2
|
+
import { map } from 'rxjs/operators';
|
|
3
|
+
|
|
4
|
+
export interface MightHaveId {
|
|
5
|
+
id?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function mapToRecord<T extends MightHaveId>(
|
|
9
|
+
state: RecordState = 'unchanged',
|
|
10
|
+
): OperatorFunction<T, IRecord<T>> {
|
|
11
|
+
return map(
|
|
12
|
+
(dto: T): IRecord<T> => ({ id: dto.id as string, dbo: dto, state }),
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface IRecord<T> {
|
|
17
|
+
readonly id: string;
|
|
18
|
+
readonly dbo?: T;
|
|
19
|
+
readonly state?: RecordState; // undefined == 'unchanged';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type RecordState =
|
|
23
|
+
| 'changed'
|
|
24
|
+
| 'creating'
|
|
25
|
+
| 'deleting'
|
|
26
|
+
| 'loading'
|
|
27
|
+
| 'unchanged'
|
|
28
|
+
| 'updating';
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.lib.base.json",
|
|
3
|
+
"exclude": [
|
|
4
|
+
"vite.config.ts",
|
|
5
|
+
"vite.config.mts",
|
|
6
|
+
"vitest.config.ts",
|
|
7
|
+
"vitest.config.mts",
|
|
8
|
+
"src/**/*.test.ts",
|
|
9
|
+
"src/**/*.spec.ts",
|
|
10
|
+
"src/**/*.test.tsx",
|
|
11
|
+
"src/**/*.spec.tsx",
|
|
12
|
+
"src/**/*.test.js",
|
|
13
|
+
"src/**/*.spec.js",
|
|
14
|
+
"src/**/*.test.jsx",
|
|
15
|
+
"src/**/*.spec.jsx",
|
|
16
|
+
"src/test-setup.ts",
|
|
17
|
+
"src/lib/testing/**/*"
|
|
18
|
+
]
|
|
19
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "../../dist/out-tsc",
|
|
5
|
+
"types": [
|
|
6
|
+
"vitest/globals",
|
|
7
|
+
"vitest/importMeta",
|
|
8
|
+
"vite/client",
|
|
9
|
+
"node",
|
|
10
|
+
"vitest"
|
|
11
|
+
]
|
|
12
|
+
},
|
|
13
|
+
"include": [
|
|
14
|
+
"vite.config.ts",
|
|
15
|
+
"vite.config.mts",
|
|
16
|
+
"vitest.config.ts",
|
|
17
|
+
"vitest.config.mts",
|
|
18
|
+
"src/**/*.test.ts",
|
|
19
|
+
"src/**/*.spec.ts",
|
|
20
|
+
"src/**/*.test.tsx",
|
|
21
|
+
"src/**/*.spec.tsx",
|
|
22
|
+
"src/**/*.test.js",
|
|
23
|
+
"src/**/*.spec.js",
|
|
24
|
+
"src/**/*.test.jsx",
|
|
25
|
+
"src/**/*.spec.jsx",
|
|
26
|
+
"src/**/*.d.ts"
|
|
27
|
+
],
|
|
28
|
+
"files": [
|
|
29
|
+
"src/test-setup.ts"
|
|
30
|
+
]
|
|
31
|
+
}
|
package/vite.config.mts
ADDED