Skip to content

Commit 4c03700

Browse files
Merge pull request #7 from typescript-package/develop
v5.0.0
2 parents 4b2582e + 71ce297 commit 4c03700

33 files changed

+611
-1376
lines changed

README.md

Lines changed: 41 additions & 250 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,8 @@ A lightweight **TypeScript** library for basic data management.
2525
- Base
2626
- [`Data`](#data)
2727
- [`Value`](#value)
28-
- Map
29-
- [`CoreMap`](#coremap)
30-
- [`DataMap`](#datamap)
31-
- [`FactoryMap`](#factorymap)
32-
- [`WeakDataMap`](#weakdatamap)
33-
- Set
34-
- [`CoreSet`](#coreset)
35-
- [`DataSet`](#dataset)
28+
- [`ImmutableData`](#immutabledata)
29+
- [`ReadonlyData`](#readonlydata)
3630
- WeakData
3731
- [`IndexedWeakData`](#indexedweakdata)
3832
- [`WeakData`](#weakdata)
@@ -48,6 +42,13 @@ A lightweight **TypeScript** library for basic data management.
4842
- [Versioning](#versioning)
4943
- [License](#license)
5044

45+
## 📦 Related Packages
46+
47+
| Package | Description |
48+
|---------------------------|---------------------------------------------------|
49+
| [`@typescript-package/map`](https://github.com/typescript-package/map) | A lightweight **TypeScript** library for enhanced `map` management. |
50+
| [`@typescript-package/set`](https://github.com/typescript-package/set) | A lightweight **TypeScript** library for enhanced `set` management. |
51+
5152
## Installation
5253

5354
```bash
@@ -66,6 +67,8 @@ import {
6667

6768
// Class.
6869
Data,
70+
ImmutableData,
71+
ReadonlyData,
6972
Value,
7073

7174
// `WeakData`.
@@ -77,33 +80,6 @@ import {
7780
} from '@typescript-package/data';
7881
```
7982

80-
`Map`.
81-
82-
```typescript
83-
import {
84-
// Abstract.
85-
CoreMap,
86-
87-
// Class.
88-
DataMap,
89-
FactoryMap,
90-
WeakDataMap,
91-
} from '@typescript-package/data';
92-
```
93-
94-
`Set`.
95-
96-
```typescript
97-
import {
98-
// Abstract.
99-
CoreSet,
100-
101-
// Class.
102-
DataSet,
103-
ImmutableSet,
104-
} from '@typescript-package/data';
105-
```
106-
10783
Constant
10884

10985
```typescript
@@ -170,11 +146,11 @@ class StringData extends Data<string> {
170146
const data = new StringData("Hello, world!");
171147

172148
// Access the current value
173-
console.log(data.value); // Output: Hello, world!
149+
console.log(data.value); // Hello, world!
174150

175151
// Update the value
176152
data.set("New value");
177-
console.log(data.value); // Output: New value
153+
console.log(data.value); // New value
178154

179155
// Destroy the value
180156
data.destroy();
@@ -187,234 +163,47 @@ The class to manage the value of generic type variable `Type`.
187163

188164
```typescript
189165
import { Value } from '@typescript-package/data';
190-
```
191-
192-
### Map
193-
194-
### `CoreMap`
195-
196-
The abstract core class is designed for building `Map` and `DataCore` related classes.
197-
198-
```typescript
199-
import { CoreMap } from '@typescript-package/data';
200-
```
201-
202-
### `DataMap`
203-
204-
The `DataMap` is a concrete class that extends `Map` and encapsulates its data within a `DataCore` store, providing additional data management capabilities.
205-
206-
```typescript
207-
import { DataMap } from '@typescript-package/data';
208-
209-
// Define a `DataCore` implementation for holding a data in `DataMap`.
210-
export class CustomMapData<Key, Value> extends Data<Map<Key, Value>> {
211-
constructor(initialValue?: Map<Key, Value>) {
212-
super(initialValue ?? new Map());
213-
}
214-
}
215-
216-
// Create a new `DataMap` instance with predefined entries and customized data holder.
217-
export const dataMap = new DataMap
218-
// <string, number, CustomMapData<string, number>> previous approach, now captured.
219-
(
220-
[
221-
["one", 1],
222-
["two", 2],
223-
["three", 3],
224-
],
225-
// new CustomMapData() // previous approach
226-
CustomMapData // new approach
227-
); // const dataMap: DataMap<string, number, CustomMapData<string, number>>
228-
229-
// Check the `CustomMapData`.
230-
console.log(`Data holder of \`CustomMapData\`:`, dataMap.data); // Output: CustomMapData {#locked: false, #value: Value}
231-
232-
// Get the `CustomMapData` value.
233-
console.log(`Data holder of \`CustomMapData\` value:`, dataMap.data.value); // Output: Map(3) {'one' => 1, 'two' => 2, 'three' => 3}
234-
235-
// Log the size of the map
236-
console.log("Size:", dataMap.size); // Output: Size: 3
237-
238-
// Get a value from the map
239-
console.log("Value for 'two':", dataMap.get("two")); // Output: Value for 'two': 2
240-
241-
// Check if a key exists
242-
console.log("Has 'three'?", dataMap.has("three")); // Output: Has 'three'? true
243-
244-
// Set a new key-value pair
245-
dataMap.set("four", 4);
246-
console.log("Size after set:", dataMap.size); // Output: Size after set: 4
247-
248-
// Iterate over entries
249-
dataMap.forEach((value, key) => console.log(`${key}: ${value}`));
250-
// Output:
251-
// one: 1
252-
// two: 2
253-
// three: 3
254-
// four: 4
255-
256-
// Delete an entry
257-
dataMap.delete("one");
258-
console.log("Size after delete:", dataMap.size); // Output: Size after delete: 3
259-
260-
// Clear the map
261-
dataMap.clear();
262-
console.log("Size after clear:", dataMap.size); // Output: Size after clear: 0
263-
264-
```
265-
266-
### `FactoryMap`
267166

268-
```typescript
269-
import { FactoryMap } from '@typescript-package/data';
270-
271-
// Define custom `Map`.
272-
export class CustomMap<Key, Value> extends Map<Key, Value> {
273-
public newMethod() {}
274-
constructor(entries?: [Key, Value][]) {
275-
super(entries);
276-
}
277-
}
278-
279-
// Define data storage to store custom map.
280-
export class TestCustomMapData<Key, Value> extends Data<CustomMap<Key, Value>> {
281-
constructor(initialValue?: CustomMap<Key, Value>) {
282-
super(initialValue ?? new CustomMap());
283-
}
284-
}
285-
286-
// Initialize the factory map with custom map and data.
287-
const factoryMap = new FactoryMap(
288-
[['a', {x: 1}], ['b', {x: 2}]],
167+
// Create a Value instance to hold a number
168+
const numValue = new Value<number>(10);
289169

290-
// Use custom `Map`
291-
CustomMap,
170+
console.log(numValue.value); // ➝ 10
171+
console.log(numValue.tag); // ➝ "Value"
292172

293-
// Use custom storage for custom map.
294-
TestCustomMapData,
295-
{
296-
// Define function for the default value.
297-
defaultValue: () => ({x: 0}),
298-
299-
// Define cloner by using the `structuredClone`.
300-
cloner: (value) => structuredClone(value),
301-
}
302-
); // const factoryMap: FactoryMap<string, { x: number; }, CustomMap<string, { x: number }>, TestCustomMapData<string, { x: number; }>>
303-
304-
console.log(factoryMap.get('c')); // { x: 0 }
305-
console.log(factoryMap.get('b')); // { x: 2 }
306-
console.log(factoryMap.get('a')); // { x: 1 }
307-
console.debug(factoryMap.sort()); // sort.
173+
// Update the stored number
174+
numValue.set(42);
308175

176+
console.log(numValue.value); // ➝ 42
309177
```
310178

311-
### `WeakDataMap`
312-
313-
The `WeakDataMap` class is a concrete class that stores data in a static `WeakMap`.
179+
### `ImmutableData`
314180

315181
```typescript
316-
import { WeakDataMap } from '@typescript-package/data';
317-
318-
// Create an instance of `WeakDataMap`.
319-
const weakDataMap = new WeakDataMap([
320-
['one', 1],
321-
['two', 2],
322-
['three', 3],
323-
]);
324-
325-
326-
// Get the value from `WeakData` static.
327-
console.log(`data: `, WeakData.get(weakDataMap.data)); // Output: Map(3) {'one' => 1, 'two' => 2, 'three' => 3}
328-
329-
// Get a value by key
330-
console.log(weakDataMap.get('two')); // Output: 2
331-
332-
// Add a new key-value pair
333-
weakDataMap.set('four', 4);
334-
335-
// Check if a key exists
336-
console.log(weakDataMap.has('four')); // Output: true
337-
338-
// Delete a key
339-
weakDataMap.delete('one');
182+
import { ImmutableData } from '@typescript-package/data';
340183

341-
// Iterate over entries
342-
for (const [key, value] of weakDataMap.entries()) {
343-
console.log(key, value);
344-
}
345-
346-
// Output:
347-
// two 2
348-
// three 3
349-
// four 4
350-
351-
```
352-
353-
### Set
184+
const immutableData = new ImmutableData(['string', 27, false]);
354185

355-
### `CoreSet`
186+
// immutableData.value[0] = 'new string'; // Cannot assign to '0' because it is a read-only property.
187+
try {
188+
(immutableData.value[0] as any) = 'new string' // Cannot assign to read only property '0' of object '[object Array]'
189+
} catch(e) {}
356190

357-
The abstract core class for building customizable `Set` and `DataCore` related classes.
358-
359-
```typescript
360-
import { CoreSet } from '@typescript-package/data';
191+
console.debug(`immutableData.value: `, immutableData.value); // ➝ ['string', 27, false]
361192
```
362193

363-
### `DataSet`
364-
365-
The `DataSet` is a concrete class that extends `CoreSet` and encapsulates its data within a `DataCore` store, providing additional data management capabilities.
194+
### `ReadonlyData`
366195

367196
```typescript
368-
import { DataSet } from '@typescript-package/data';
197+
import { ReadonlyData } from '@typescript-package/data';
369198

370-
// Define a `DataCore` implementation for holding a data in `DataSet`.
371-
export class CustomSetData<Type> extends Data<Set<Type>> {
372-
constructor(initialValue?: Set<Type>, ...args: any[]) {
373-
super(initialValue ?? new Set());
374-
console.log(`...args: any[]`, args);
375-
}
376-
}
199+
let readonlyData = new ReadonlyData(['string', 27, false]);
377200

378-
// Create a new `DataSet` instance with predefined entries and customized data holder.
379-
export const dataSet = new DataSet
380-
(
381-
[0, 27, 37, 47],
382-
[CustomSetData, 'a', 'b', 1]
383-
);
201+
console.log(`readonlyData: `, readonlyData);
384202

385-
console.debug(`dataSet`, dataSet);
203+
// readonlyData.value[0] = 'new string'; // Cannot assign to '0' because it is a read-only property.
204+
(readonlyData.value[0] as any) = 'new string' // Can assign with using `any`.
386205

387-
// Check the `CustomSetData`.
388-
console.log(`Data holder of \`CustomSetData\`:`, dataSet.data); // Output: CustomSetData {#locked: false, #value: Value}
389-
390-
// Log the size of the set
391-
console.log("Size:", dataSet.size); // Output: Size: 4
392-
393-
// Check if a value exists
394-
console.log("Has 27?", dataSet.has(27)); // Output: Has '27'? true
395-
396-
// Add a new value
397-
dataSet.add(57);
398-
console.log("Size after add:", dataSet.size); // Output: Size after add: 5
399-
400-
// Iterate over entries
401-
dataSet.forEach(value => console.log(`${value}`));
402-
// Output:
403-
// 0
404-
// 27
405-
// 37
406-
// 47
407-
// 57
408-
409-
console.debug(`SymbolValue`, dataSet[SymbolValue]());
410-
411-
// Delete an entry
412-
dataSet.delete(0);
413-
console.log("Size after delete:", dataSet.size); // Output: Size after delete: 4
414-
415-
// Clear the set
416-
dataSet.clear();
417-
console.log("Size after clear:", dataSet.size); // Output: Size after clear: 0
206+
console.log(`readonlyData.value: `, readonlyData.value); // ➝ ['new string', 27, false]
418207
```
419208

420209
### WeakData
@@ -438,8 +227,8 @@ export const profileData3 = new IndexedWeakData({ id: 3, age: 227, score: 1300 }
438227
export const profileData4 = new IndexedWeakData({ id: 4, age: 327, score: 1400 } as Profile, 'id');
439228

440229
// Get the value by using index.
441-
console.log(`profileData1: `, profileData1.getByIndex(1)); // Output: {id: 1, age: 27, score: 1100}
442-
console.log(`profileData3: `, profileData3.getByIndex(3)); // Output: {id: 3, age: 227, score: 1300}
230+
console.log(`profileData1: `, profileData1.getByIndex(1)); // {id: 1, age: 27, score: 1100}
231+
console.log(`profileData3: `, profileData3.getByIndex(3)); // {id: 3, age: 227, score: 1300}
443232
```
444233

445234
### `WeakData`
@@ -460,11 +249,11 @@ export class StringWeakData extends WeakData<string> {
460249
export const data = new StringWeakData("Hello, world!");
461250

462251
// Access the current value
463-
console.log(data.value); // Output: Hello, world!
252+
console.log(data.value); // Hello, world!
464253

465254
// Update the value
466255
data.set("New value");
467-
console.log(data.value); // Output: New value
256+
console.log(data.value); // New value
468257

469258
// Destroy the value
470259
data.destroy();
@@ -543,6 +332,8 @@ MIT © typescript-package ([license][typescript-package-license])
543332

544333
- **[@typescript-package/affix](https://github.com/typescript-package/affix)**: A **lightweight TypeScript** library for the affix - prefix and suffix.
545334
- **[@typescript-package/are](https://github.com/typescript-package/are)**: Type-safe `are` checkers for validating value types in TypeScript.
335+
336+
546337
- **[@typescript-package/descriptor](https://github.com/typescript-package/descriptor)**: A **lightweight TypeScript** library for property descriptor.
547338
- **[@typescript-package/guard](https://github.com/typescript-package/guard)**: Type-safe guards for guarding the value types in TypeScript.c
548339
- **[@typescript-package/history](https://github.com/typescript-package/history)**: A **TypeScript** package for tracking history of values.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@typescript-package/data",
3-
"version": "4.0.0-0",
3+
"version": "5.0.0",
44
"author": "wwwdev.io <dev@wwwdev.io>",
55
"description": "A lightweight TypeScript library for basic data management.",
66
"license": "MIT",

0 commit comments

Comments
 (0)