@ivan-angelkoski/counter 1.0.10 → 1.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/dist/index.cjs +1 -1
- package/dist/index.d.cts +84 -5
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +84 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e=class{count
|
|
1
|
+
var e=class{count;initialValue;min;max;step;listeners=new Set;constructor(e={}){let{initialValue:t=0,min:n=-1/0,max:r=1/0,step:i=1}=e;if(n>r)throw Error(`min cannot be greater than max`);this.min=n,this.max=r,this.step=i,this.initialValue=this.clamp(t),this.count=this.initialValue}clamp(e){return Math.min(Math.max(e,this.min),this.max)}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}notify(){for(let e of this.listeners)e(this.getState())}getState(){return{count:this.count}}getCount(){return this.count}increment(e){let t=e??this.step;this.count=this.clamp(this.count+t),this.notify()}decrement(e){let t=e??this.step;this.count=this.clamp(this.count-t),this.notify()}set(e){this.count=this.clamp(e),this.notify()}reset(e){this.count=this.clamp(e??this.initialValue),this.notify()}getMin(){return this.min}getMax(){return this.max}getStep(){return this.step}getInitialValue(){return this.initialValue}};exports.Counter=e;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,20 +1,99 @@
|
|
|
1
1
|
//#region src/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Represents the state of the counter
|
|
4
|
+
*/
|
|
2
5
|
type State = {
|
|
3
6
|
count: number;
|
|
4
7
|
};
|
|
8
|
+
/**
|
|
9
|
+
* A function that listens to state changes
|
|
10
|
+
*/
|
|
5
11
|
type Listener = (state: State) => void;
|
|
12
|
+
/**
|
|
13
|
+
* Configuration options for the Counter
|
|
14
|
+
*/
|
|
15
|
+
type CounterOptions = {
|
|
16
|
+
/** Initial value for the counter (default: 0) */initialValue?: number; /** Minimum allowed value (default: -Infinity) */
|
|
17
|
+
min?: number; /** Maximum allowed value (default: Infinity) */
|
|
18
|
+
max?: number; /** Default step for increment/decrement (default: 1) */
|
|
19
|
+
step?: number;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* A flexible counter class with subscription support and configurable boundaries
|
|
23
|
+
*/
|
|
6
24
|
declare class Counter {
|
|
7
25
|
private count;
|
|
26
|
+
private readonly initialValue;
|
|
27
|
+
private readonly min;
|
|
28
|
+
private readonly max;
|
|
29
|
+
private readonly step;
|
|
8
30
|
private listeners;
|
|
9
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new Counter instance
|
|
33
|
+
* @param options - Configuration options for the counter
|
|
34
|
+
*/
|
|
35
|
+
constructor(options?: CounterOptions);
|
|
36
|
+
/**
|
|
37
|
+
* Clamps a value within the min/max boundaries
|
|
38
|
+
*/
|
|
39
|
+
private clamp;
|
|
40
|
+
/**
|
|
41
|
+
* Subscribe to state changes
|
|
42
|
+
* @param listener - The listener function to call on state changes
|
|
43
|
+
* @returns An unsubscribe function
|
|
44
|
+
*/
|
|
10
45
|
subscribe(listener: Listener): () => void;
|
|
46
|
+
/**
|
|
47
|
+
* Notify all listeners of a state change
|
|
48
|
+
*/
|
|
11
49
|
private notify;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Get the current state object
|
|
52
|
+
* @returns The current state containing the count
|
|
53
|
+
*/
|
|
54
|
+
getState(): State;
|
|
55
|
+
/**
|
|
56
|
+
* Get the current count value
|
|
57
|
+
* @returns The current count number
|
|
58
|
+
*/
|
|
59
|
+
getCount(): number;
|
|
60
|
+
/**
|
|
61
|
+
* Increment the counter
|
|
62
|
+
* @param amount - The amount to increment by (default: configured step)
|
|
63
|
+
*/
|
|
15
64
|
increment(amount?: number): void;
|
|
65
|
+
/**
|
|
66
|
+
* Decrement the counter
|
|
67
|
+
* @param amount - The amount to decrement by (default: configured step)
|
|
68
|
+
*/
|
|
16
69
|
decrement(amount?: number): void;
|
|
70
|
+
/**
|
|
71
|
+
* Set the counter to a specific value
|
|
72
|
+
* @param value - The value to set the counter to (will be clamped to min/max)
|
|
73
|
+
*/
|
|
74
|
+
set(value: number): void;
|
|
75
|
+
/**
|
|
76
|
+
* Reset the counter to the initial value or a new value
|
|
77
|
+
* @param value - Optional new value to reset to (default: initial value)
|
|
78
|
+
*/
|
|
79
|
+
reset(value?: number): void;
|
|
80
|
+
/**
|
|
81
|
+
* Get the configured minimum value
|
|
82
|
+
*/
|
|
83
|
+
getMin(): number;
|
|
84
|
+
/**
|
|
85
|
+
* Get the configured maximum value
|
|
86
|
+
*/
|
|
87
|
+
getMax(): number;
|
|
88
|
+
/**
|
|
89
|
+
* Get the configured step value
|
|
90
|
+
*/
|
|
91
|
+
getStep(): number;
|
|
92
|
+
/**
|
|
93
|
+
* Get the initial value the counter was created with
|
|
94
|
+
*/
|
|
95
|
+
getInitialValue(): number;
|
|
17
96
|
}
|
|
18
97
|
//#endregion
|
|
19
|
-
export { Counter };
|
|
98
|
+
export { Counter, CounterOptions, Listener, State };
|
|
20
99
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";;AAGA;;KAAY,KAAA;EACV,KAAA;AAAA;AAMF;;;AAAA,KAAY,QAAA,IAAY,KAAA,EAAO,KAAA;;AAK/B;;KAAY,cAAA;EAAc,iDAExB,YAAA,WAEA;EAAA,GAAA,WAIA;EAFA,GAAA,WAEI;EAAJ,IAAA;AAAA;;;;cAMW,OAAA;EAAA,QACH,KAAA;EAAA,iBACS,YAAA;EAAA,iBACA,GAAA;EAAA,iBACA,GAAA;EAAA,iBACA,IAAA;EAAA,QACT,SAAA;EADS;;;;cAOL,OAAA,GAAS,cAAA;EAiBb;;;EAAA,QAAA,KAAA;EAoBA;;;;;EAXR,SAAA,CAAU,QAAA,EAAU,QAAA;EA+CpB;;;EAAA,QApCQ,MAAA;EAuDR;;;;EA7CA,QAAA,CAAA,GAAY,KAAA;EA0EZ;;;;EAlEA,QAAA,CAAA;;;;;EAQA,SAAA,CAAU,MAAA;;;;;EAUV,SAAA,CAAU,MAAA;;;;;EAUV,GAAA,CAAI,KAAA;;;;;EASJ,KAAA,CAAM,KAAA;;;;EAQN,MAAA,CAAA;;;;EAOA,MAAA,CAAA;;;;EAOA,OAAA,CAAA;;;;EAOA,eAAA,CAAA;AAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,99 @@
|
|
|
1
1
|
//#region src/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Represents the state of the counter
|
|
4
|
+
*/
|
|
2
5
|
type State = {
|
|
3
6
|
count: number;
|
|
4
7
|
};
|
|
8
|
+
/**
|
|
9
|
+
* A function that listens to state changes
|
|
10
|
+
*/
|
|
5
11
|
type Listener = (state: State) => void;
|
|
12
|
+
/**
|
|
13
|
+
* Configuration options for the Counter
|
|
14
|
+
*/
|
|
15
|
+
type CounterOptions = {
|
|
16
|
+
/** Initial value for the counter (default: 0) */initialValue?: number; /** Minimum allowed value (default: -Infinity) */
|
|
17
|
+
min?: number; /** Maximum allowed value (default: Infinity) */
|
|
18
|
+
max?: number; /** Default step for increment/decrement (default: 1) */
|
|
19
|
+
step?: number;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* A flexible counter class with subscription support and configurable boundaries
|
|
23
|
+
*/
|
|
6
24
|
declare class Counter {
|
|
7
25
|
private count;
|
|
26
|
+
private readonly initialValue;
|
|
27
|
+
private readonly min;
|
|
28
|
+
private readonly max;
|
|
29
|
+
private readonly step;
|
|
8
30
|
private listeners;
|
|
9
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new Counter instance
|
|
33
|
+
* @param options - Configuration options for the counter
|
|
34
|
+
*/
|
|
35
|
+
constructor(options?: CounterOptions);
|
|
36
|
+
/**
|
|
37
|
+
* Clamps a value within the min/max boundaries
|
|
38
|
+
*/
|
|
39
|
+
private clamp;
|
|
40
|
+
/**
|
|
41
|
+
* Subscribe to state changes
|
|
42
|
+
* @param listener - The listener function to call on state changes
|
|
43
|
+
* @returns An unsubscribe function
|
|
44
|
+
*/
|
|
10
45
|
subscribe(listener: Listener): () => void;
|
|
46
|
+
/**
|
|
47
|
+
* Notify all listeners of a state change
|
|
48
|
+
*/
|
|
11
49
|
private notify;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Get the current state object
|
|
52
|
+
* @returns The current state containing the count
|
|
53
|
+
*/
|
|
54
|
+
getState(): State;
|
|
55
|
+
/**
|
|
56
|
+
* Get the current count value
|
|
57
|
+
* @returns The current count number
|
|
58
|
+
*/
|
|
59
|
+
getCount(): number;
|
|
60
|
+
/**
|
|
61
|
+
* Increment the counter
|
|
62
|
+
* @param amount - The amount to increment by (default: configured step)
|
|
63
|
+
*/
|
|
15
64
|
increment(amount?: number): void;
|
|
65
|
+
/**
|
|
66
|
+
* Decrement the counter
|
|
67
|
+
* @param amount - The amount to decrement by (default: configured step)
|
|
68
|
+
*/
|
|
16
69
|
decrement(amount?: number): void;
|
|
70
|
+
/**
|
|
71
|
+
* Set the counter to a specific value
|
|
72
|
+
* @param value - The value to set the counter to (will be clamped to min/max)
|
|
73
|
+
*/
|
|
74
|
+
set(value: number): void;
|
|
75
|
+
/**
|
|
76
|
+
* Reset the counter to the initial value or a new value
|
|
77
|
+
* @param value - Optional new value to reset to (default: initial value)
|
|
78
|
+
*/
|
|
79
|
+
reset(value?: number): void;
|
|
80
|
+
/**
|
|
81
|
+
* Get the configured minimum value
|
|
82
|
+
*/
|
|
83
|
+
getMin(): number;
|
|
84
|
+
/**
|
|
85
|
+
* Get the configured maximum value
|
|
86
|
+
*/
|
|
87
|
+
getMax(): number;
|
|
88
|
+
/**
|
|
89
|
+
* Get the configured step value
|
|
90
|
+
*/
|
|
91
|
+
getStep(): number;
|
|
92
|
+
/**
|
|
93
|
+
* Get the initial value the counter was created with
|
|
94
|
+
*/
|
|
95
|
+
getInitialValue(): number;
|
|
17
96
|
}
|
|
18
97
|
//#endregion
|
|
19
|
-
export { Counter };
|
|
98
|
+
export { Counter, CounterOptions, Listener, State };
|
|
20
99
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";;AAGA;;KAAY,KAAA;EACV,KAAA;AAAA;AAMF;;;AAAA,KAAY,QAAA,IAAY,KAAA,EAAO,KAAA;;AAK/B;;KAAY,cAAA;EAAc,iDAExB,YAAA,WAEA;EAAA,GAAA,WAIA;EAFA,GAAA,WAEI;EAAJ,IAAA;AAAA;;;;cAMW,OAAA;EAAA,QACH,KAAA;EAAA,iBACS,YAAA;EAAA,iBACA,GAAA;EAAA,iBACA,GAAA;EAAA,iBACA,IAAA;EAAA,QACT,SAAA;EADS;;;;cAOL,OAAA,GAAS,cAAA;EAiBb;;;EAAA,QAAA,KAAA;EAoBA;;;;;EAXR,SAAA,CAAU,QAAA,EAAU,QAAA;EA+CpB;;;EAAA,QApCQ,MAAA;EAuDR;;;;EA7CA,QAAA,CAAA,GAAY,KAAA;EA0EZ;;;;EAlEA,QAAA,CAAA;;;;;EAQA,SAAA,CAAU,MAAA;;;;;EAUV,SAAA,CAAU,MAAA;;;;;EAUV,GAAA,CAAI,KAAA;;;;;EASJ,KAAA,CAAM,KAAA;;;;EAQN,MAAA,CAAA;;;;EAOA,MAAA,CAAA;;;;EAOA,OAAA,CAAA;;;;EAOA,eAAA,CAAA;AAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e=class{count
|
|
1
|
+
var e=class{count;initialValue;min;max;step;listeners=new Set;constructor(e={}){let{initialValue:t=0,min:n=-1/0,max:r=1/0,step:i=1}=e;if(n>r)throw Error(`min cannot be greater than max`);this.min=n,this.max=r,this.step=i,this.initialValue=this.clamp(t),this.count=this.initialValue}clamp(e){return Math.min(Math.max(e,this.min),this.max)}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}notify(){for(let e of this.listeners)e(this.getState())}getState(){return{count:this.count}}getCount(){return this.count}increment(e){let t=e??this.step;this.count=this.clamp(this.count+t),this.notify()}decrement(e){let t=e??this.step;this.count=this.clamp(this.count-t),this.notify()}set(e){this.count=this.clamp(e),this.notify()}reset(e){this.count=this.clamp(e??this.initialValue),this.notify()}getMin(){return this.min}getMax(){return this.max}getStep(){return this.step}getInitialValue(){return this.initialValue}};export{e as Counter};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["type State = {\n count: number;\n};\n\
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * Represents the state of the counter\n */\nexport type State = {\n count: number;\n};\n\n/**\n * A function that listens to state changes\n */\nexport type Listener = (state: State) => void;\n\n/**\n * Configuration options for the Counter\n */\nexport type CounterOptions = {\n /** Initial value for the counter (default: 0) */\n initialValue?: number;\n /** Minimum allowed value (default: -Infinity) */\n min?: number;\n /** Maximum allowed value (default: Infinity) */\n max?: number;\n /** Default step for increment/decrement (default: 1) */\n step?: number;\n};\n\n/**\n * A flexible counter class with subscription support and configurable boundaries\n */\nexport class Counter {\n private count: number;\n private readonly initialValue: number;\n private readonly min: number;\n private readonly max: number;\n private readonly step: number;\n private listeners: Set<Listener> = new Set();\n\n /**\n * Creates a new Counter instance\n * @param options - Configuration options for the counter\n */\n constructor(options: CounterOptions = {}) {\n const { initialValue = 0, min = -Infinity, max = Infinity, step = 1 } = options;\n\n if (min > max) {\n throw new Error(\"min cannot be greater than max\");\n }\n\n this.min = min;\n this.max = max;\n this.step = step;\n this.initialValue = this.clamp(initialValue);\n this.count = this.initialValue;\n }\n\n /**\n * Clamps a value within the min/max boundaries\n */\n private clamp(value: number): number {\n return Math.min(Math.max(value, this.min), this.max);\n }\n\n /**\n * Subscribe to state changes\n * @param listener - The listener function to call on state changes\n * @returns An unsubscribe function\n */\n subscribe(listener: Listener) {\n this.listeners.add(listener);\n\n return () => {\n this.listeners.delete(listener);\n };\n }\n\n /**\n * Notify all listeners of a state change\n */\n private notify() {\n for (const listener of this.listeners) {\n listener(this.getState());\n }\n }\n\n /**\n * Get the current state object\n * @returns The current state containing the count\n */\n getState(): State {\n return { count: this.count };\n }\n\n /**\n * Get the current count value\n * @returns The current count number\n */\n getCount(): number {\n return this.count;\n }\n\n /**\n * Increment the counter\n * @param amount - The amount to increment by (default: configured step)\n */\n increment(amount?: number) {\n const incrementBy = amount ?? this.step;\n this.count = this.clamp(this.count + incrementBy);\n this.notify();\n }\n\n /**\n * Decrement the counter\n * @param amount - The amount to decrement by (default: configured step)\n */\n decrement(amount?: number) {\n const decrementBy = amount ?? this.step;\n this.count = this.clamp(this.count - decrementBy);\n this.notify();\n }\n\n /**\n * Set the counter to a specific value\n * @param value - The value to set the counter to (will be clamped to min/max)\n */\n set(value: number) {\n this.count = this.clamp(value);\n this.notify();\n }\n\n /**\n * Reset the counter to the initial value or a new value\n * @param value - Optional new value to reset to (default: initial value)\n */\n reset(value?: number) {\n this.count = this.clamp(value ?? this.initialValue);\n this.notify();\n }\n\n /**\n * Get the configured minimum value\n */\n getMin(): number {\n return this.min;\n }\n\n /**\n * Get the configured maximum value\n */\n getMax(): number {\n return this.max;\n }\n\n /**\n * Get the configured step value\n */\n getStep(): number {\n return this.step;\n }\n\n /**\n * Get the initial value the counter was created with\n */\n get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,KAAK"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ivan-angelkoski/counter",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Test counter package with changesets",
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"description": "Test counter package with changesets automation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
7
7
|
"module": "dist/index.js",
|