notu 0.1.0

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,71 @@
1
+ 'use strict';
2
+
3
+ import Note from './Note';
4
+ import ModelWithState from './ModelWithState';
5
+ import Tag from './Tag';
6
+
7
+
8
+ export default class NoteTag extends ModelWithState<NoteTag> {
9
+ private _noteId: number = 0;
10
+ get noteId(): number { return this._noteId; }
11
+ set noteId(value: number) {
12
+ if (value !== this._noteId) {
13
+ this._noteId = value;
14
+ if (value !== this.note?.id ?? 0)
15
+ this._note = null;
16
+ if (this.isClean)
17
+ this.dirty();
18
+ }
19
+ }
20
+
21
+ private _note: Note = null;
22
+ get note(): Note { return this._note; }
23
+ set note(value: Note) {
24
+ this._note = value;
25
+ this.noteId = value?.id ?? 0;
26
+ }
27
+
28
+
29
+ private _tagId: number = 0;
30
+ get tagId(): number { return this._tagId; }
31
+ set tagId(value: number) {
32
+ if (value !== this._tagId) {
33
+ this._tagId = value;
34
+ if (value !== this.tag?.id ?? 0)
35
+ this._tag = null;
36
+ if (this.isClean)
37
+ this.dirty();
38
+ }
39
+ }
40
+
41
+ private _tag: Tag = null;
42
+ get tag(): Tag { return this._tag; }
43
+ set tag(value: Tag) {
44
+ this._tag = value;
45
+ this.tagId = value?.id ?? 0;
46
+ }
47
+
48
+
49
+ duplicate(): NoteTag {
50
+ const output = new NoteTag();
51
+ output.noteId = this.noteId;
52
+ output.tagId = this.tagId;
53
+ return output;
54
+ }
55
+
56
+
57
+ validate(throwError: boolean = false): boolean {
58
+ let output = null;
59
+
60
+ if (this.noteId <= 0 && !this.isNew)
61
+ output = 'NoteTag noteId must be greater than zero';
62
+ else if (this.tagId <= 0)
63
+ output = 'NoteTag tagId must be greater than zero';
64
+ else if (this.noteId == this.tagId)
65
+ output = 'NoteTag cannot link a note to its own tag';
66
+
67
+ if (throwError && output != null)
68
+ throw Error(output);
69
+ return output == null;
70
+ }
71
+ }
@@ -0,0 +1,53 @@
1
+ import { expect, test } from 'vitest';
2
+ import Space from './Space';
3
+
4
+
5
+ test('Gets initiated as new', () => {
6
+ const space = new Space();
7
+ expect(space.isNew).toBe(true);
8
+ });
9
+
10
+
11
+ test('Set name marks space as dirty if currently clean', () => {
12
+ const space = new Space().clean();
13
+ space.name = 'asdf';
14
+ expect(space.isDirty).toBe(true);
15
+ });
16
+
17
+ test('Set name doesnt change space state if new', () => {
18
+ const space = new Space().new();
19
+ space.name = 'asdf';
20
+ expect(space.isNew).toBe(true);
21
+ });
22
+
23
+ test('Set name doesnt change space state if value not different', () => {
24
+ const space = new Space().clean();
25
+ space.name = '';
26
+ expect(space.isClean).toBe(true);
27
+ });
28
+
29
+
30
+ test('Space can be initiated with name in constructor', () => {
31
+ const space = new Space('hello');
32
+ expect(space.name).toBe('hello');
33
+ });
34
+
35
+ test('can duplicate itself', () => {
36
+ const space = new Space('hello').clean();
37
+ const copy = space.duplicate();
38
+ expect(copy.id).toBe(space.id);
39
+ expect(copy.name).toBe(space.name);
40
+ expect(copy.state).toBe(space.state);
41
+ });
42
+
43
+ test('validate fails if not new and id <= 0', () => {
44
+ const model = new Space().clean();
45
+ model.id = 0;
46
+ expect(model.validate()).toBe(false);
47
+ });
48
+
49
+ test('validate throws error if arg set to true', () => {
50
+ const model = new Space().clean();
51
+ model.id = 0;
52
+ expect(() => model.validate(true)).toThrowError();
53
+ });
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ import ModelWithState from './ModelWithState';
4
+
5
+
6
+ export default class Space extends ModelWithState<Space> {
7
+ id: number = 0;
8
+
9
+
10
+ private _name: string = '';
11
+ get name(): string { return this._name; }
12
+ set name(value: string) {
13
+ if (value !== this._name) {
14
+ this._name = value;
15
+ if (this.isClean)
16
+ this.dirty();
17
+ }
18
+ }
19
+
20
+
21
+ constructor(name: string = '') {
22
+ super();
23
+ this._name = name;
24
+ }
25
+
26
+
27
+ duplicate(): Space {
28
+ const output = new Space();
29
+ output.id = this.id;
30
+ output.name = this.name;
31
+ output.state = this.state;
32
+ return output;
33
+ }
34
+
35
+
36
+ validate(throwError: boolean = false): boolean {
37
+ let output = null;
38
+
39
+ if (!this.isNew && this.id <= 0)
40
+ output = 'Space id must be greater than zero if in non-new state.';
41
+
42
+ if (throwError && output != null)
43
+ throw Error(output);
44
+ return output == null;
45
+ }
46
+ }
@@ -0,0 +1,71 @@
1
+ import { expect, test } from 'vitest';
2
+ import Tag from './Tag';
3
+
4
+
5
+ test('Gets initiated as new', () => {
6
+ const tag = new Tag();
7
+ expect(tag.isNew).toBe(true);
8
+ });
9
+
10
+
11
+ test('Set id marks tag as dirty if currently clean', () => {
12
+ const tag = new Tag().clean();
13
+ tag.id = 123;
14
+ expect(tag.isDirty).toBe(true);
15
+ });
16
+
17
+ test('Set id doesnt change tag state if new', () => {
18
+ const tag = new Tag().new();
19
+ tag.id = 123;
20
+ expect(tag.isNew).toBe(true);
21
+ });
22
+
23
+ test('Set id doesnt change tag state if value not different', () => {
24
+ const tag = new Tag().clean();
25
+ tag.id = 0;
26
+ expect(tag.isClean).toBe(true);
27
+ });
28
+
29
+
30
+ test('Set name marks tag as dirty if currently clean', () => {
31
+ const tag = new Tag().clean();
32
+ tag.name = 'asdf';
33
+ expect(tag.isDirty).toBe(true);
34
+ });
35
+
36
+ test('Set name doesnt change tag state if new', () => {
37
+ const tag = new Tag().new();
38
+ tag.name = 'asdf';
39
+ expect(tag.isNew).toBe(true);
40
+ });
41
+
42
+ test('Set name doesnt change tag state if value not different', () => {
43
+ const tag = new Tag().clean();
44
+ tag.name = '';
45
+ expect(tag.isClean).toBe(true);
46
+ });
47
+
48
+ test('Tag can be initiated with name in constructor', () => {
49
+ const tag = new Tag('hello');
50
+ expect(tag.name).toBe('hello');
51
+ });
52
+
53
+ test('can duplicate itself', () => {
54
+ const tag = new Tag('hello');
55
+ const copy = tag.duplicate();
56
+ expect(copy.id).toBe(tag.id);
57
+ expect(copy.name).toBe(tag.name);
58
+ expect(copy.state).toBe(tag.state);
59
+ });
60
+
61
+ test('validate fails if not new and id <= 0', () => {
62
+ const model = new Tag().clean();
63
+ model.id = 0;
64
+ expect(model.validate()).toBe(false);
65
+ });
66
+
67
+ test('validate throws error if arg set to true', () => {
68
+ const model = new Tag().clean();
69
+ model.id = 0;
70
+ expect(() => model.validate(true)).toThrowError();
71
+ });
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ import ModelWithState from './ModelWithState';
4
+
5
+
6
+ export default class Tag extends ModelWithState<Tag> {
7
+ private _id: number = 0;
8
+ get id(): number { return this._id; }
9
+ set id(value: number) {
10
+ if (value !== this._id) {
11
+ this._id = value;
12
+ if (this.isClean)
13
+ this.dirty();
14
+ }
15
+ }
16
+
17
+
18
+ private _name: string = '';
19
+ get name(): string { return this._name; }
20
+ set name(value: string) {
21
+ if (value !== this._name) {
22
+ this._name = value;
23
+ if (this.isClean)
24
+ this.dirty();
25
+ }
26
+ }
27
+
28
+
29
+ constructor(name: string = '') {
30
+ super();
31
+ this._name = name;
32
+ }
33
+
34
+
35
+ duplicate(): Tag {
36
+ const output = new Tag();
37
+ output.id = this.id;
38
+ output.name = this.name;
39
+ output.state = this.state;
40
+ return output;
41
+ }
42
+
43
+
44
+ validate(throwError: boolean = false): boolean {
45
+ let output = null;
46
+
47
+ if (!this.isNew && this.id <= 0)
48
+ output = 'Tag id must be greater than zero if in non-new state.';
49
+
50
+ if (throwError && output != null)
51
+ throw Error(output);
52
+ return output == null;
53
+ }
54
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "useDefineForClassFields": true,
5
+ "module": "ESNext",
6
+ "lib": ["ES2020", "DOM", "DOM.Iterable"],
7
+ "skipLibCheck": true,
8
+
9
+ /* Bundler mode */
10
+ "moduleResolution": "bundler",
11
+ "allowImportingTsExtensions": true,
12
+ "resolveJsonModule": true,
13
+ "isolatedModules": true,
14
+ "noEmit": true,
15
+
16
+ /* Linting */
17
+ "strict": false,
18
+ "noUnusedLocals": true,
19
+ "noUnusedParameters": false,
20
+ "noFallthroughCasesInSwitch": true
21
+ },
22
+ "include": ["src"]
23
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { defineConfig } from 'vite';
2
+
3
+ export default defineConfig({
4
+ build: {
5
+ lib: {
6
+ name: 'notu',
7
+ entry: './src/index.ts'
8
+ }
9
+ }
10
+ });