-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2-record.js
More file actions
80 lines (69 loc) · 2.03 KB
/
Copy path2-record.js
File metadata and controls
80 lines (69 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
class Record {
static immutable(fields) {
return Record.#build(fields, false);
}
static mutable(fields) {
return Record.#build(fields, true);
}
static #build(fields, isMutable) {
const fieldSet = new Set(fields);
class Struct {
static fields = Object.freeze(fields.slice());
static mutable = isMutable;
static create(props) {
for (const field of fields) {
if (!Reflect.has(props, field)) {
throw new Error(`Missing field: ${field}`);
}
}
for (const key in props) {
if (!fieldSet.has(key)) {
throw new Error(`Unexpected field: ${key}`);
}
}
const obj = Object.fromEntries(
fields.map(field => [field, props[field]])
);
return isMutable ? Object.seal(obj) : Object.freeze(obj);
}
}
return Struct;
}
static update(instance, updates) {
if (Object.isFrozen(instance)) {
throw new Error('Cannot mutate immutable Record');
}
for (const key of Object.keys(updates)) {
if (Reflect.has(instance, key)) {
instance[key] = updates[key];
}
}
return instance;
}
static fork(instance, updates) {
const copy = { ...instance, ...updates };
return Object.isFrozen(instance) ? Object.freeze(copy) : Object.seal(copy);
}
}
// Оновлений приклад використання
const City = Record.immutable(['name']);
const User = Record.immutable(['id', 'name', 'city', 'email']);
const rome = City.create({ name: 'Rome' });
const marcus = User.create({
id: 1,
name: 'Marcus',
city: rome,
email: 'marcus@metarhia.com'
});
const marcusUpdated = Record.fork(marcus, { name: 'Marcus Aurelius' });
const lucius = Record.fork(marcusUpdated, {
name: 'Lucius Verus',
email: 'lucius@metarhia.com'
});
console.log({ marcus, marcusUpdated, lucius });
try {
Record.update(marcus, { name: 'FAIL' });
} catch (err) {
console.error('\nError trying to update immutable record:', err.message);
}