@yaks/copy 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +109 -2
- package/_dist/locales.d.ts +50 -0
- package/_dist/locales.d.ts.map +1 -0
- package/_dist/mod.d.ts +122 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/translation.d.ts +37 -0
- package/_dist/translation.d.ts.map +1 -0
- package/deno.json +1 -0
- package/locales.js +211 -0
- package/locales.js.map +1 -0
- package/locales.ts +292 -0
- package/mod.js +248 -0
- package/mod.js.map +1 -0
- package/mod.ts +337 -0
- package/package.json +14 -4
- package/translation.js +220 -0
- package/translation.js.map +1 -0
- package/translation.ts +277 -0
package/mod.js
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/copy — an app's copy: the words its pages show, kept in `copy.json` at
|
|
3
|
+
* the app's root as keyed snippets, apart from the markup that shows them.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { fill, parse, read, set } from '@yaks/copy'
|
|
7
|
+
*
|
|
8
|
+
* let copy = parse('{\n "hero": {\n "title": "Build, host and share web apps"\n }\n}\n')
|
|
9
|
+
* read(copy, 'hero.title') // 'Build, host and share web apps'
|
|
10
|
+
* fill('Your first space is %{price}.', { price: 'free' }) // ['Your first space is ', 'free', '.']
|
|
11
|
+
* set('{}', { 'hero.kicker': 'yaks.app' }) // '{\n "hero.kicker": "yaks.app"\n}\n'
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* Pure logic and data: it takes and returns strings and values, and reads no
|
|
15
|
+
* file and touches no page.
|
|
16
|
+
*
|
|
17
|
+
* @module
|
|
18
|
+
*/ /** A copy file's value: snippets and lists of them by key, and nested
|
|
19
|
+
* objects of more. */ /** The attributes an element names its copy with. */ export let names = {
|
|
20
|
+
text: 'data-copy',
|
|
21
|
+
slot: 'data-copy-slot',
|
|
22
|
+
list: 'data-copy-list',
|
|
23
|
+
attr: (name)=>`data-copy-${name}`
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The copy an element's attributes name, from a DOM element's `attributes` or
|
|
27
|
+
* a plain object of them.
|
|
28
|
+
*
|
|
29
|
+
* ```ts
|
|
30
|
+
* marks({ 'data-copy': 'cost.plans', 'data-copy-slot': 'plans', href: '/pricing' })
|
|
31
|
+
* // { text: 'cost.plans', slot: 'plans', attrs: {} }
|
|
32
|
+
* ```
|
|
33
|
+
*/ export let marks = (attributes)=>{
|
|
34
|
+
let pairs = Symbol.iterator in attributes ? [
|
|
35
|
+
...attributes
|
|
36
|
+
].map(({ name, value })=>[
|
|
37
|
+
name,
|
|
38
|
+
value
|
|
39
|
+
]) : Object.entries(attributes);
|
|
40
|
+
let found = {
|
|
41
|
+
attrs: {}
|
|
42
|
+
};
|
|
43
|
+
let prefix = names.attr('');
|
|
44
|
+
for (let [name, value] of pairs){
|
|
45
|
+
if (name == names.text) found.text = value;
|
|
46
|
+
else if (name == names.slot) found.slot = value;
|
|
47
|
+
else if (name == names.list) found.list = value;
|
|
48
|
+
else if (name.startsWith(prefix) && ![
|
|
49
|
+
'data-copy-languages',
|
|
50
|
+
'data-copy-suggest'
|
|
51
|
+
].includes(name)) {
|
|
52
|
+
found.attrs[name.slice(prefix.length)] = value;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return found;
|
|
56
|
+
};
|
|
57
|
+
let object = (value)=>typeof value == 'object' && value != null && !Array.isArray(value);
|
|
58
|
+
let list = (value)=>Array.isArray(value) && value.every((item)=>typeof item == 'string');
|
|
59
|
+
let check = (value, path, file)=>{
|
|
60
|
+
if (!object(value)) {
|
|
61
|
+
throw new Error(`${file}: ${path || 'the file'} is not an object of snippets`);
|
|
62
|
+
}
|
|
63
|
+
for (let [key, inner] of Object.entries(value)){
|
|
64
|
+
let at = path ? `${path}.${key}` : key;
|
|
65
|
+
if (Array.isArray(inner) && !list(inner)) {
|
|
66
|
+
throw new Error(`${file}: ${at} is a list of something other than text`);
|
|
67
|
+
}
|
|
68
|
+
if (typeof inner != 'string' && !Array.isArray(inner)) {
|
|
69
|
+
check(inner, at, file);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return value;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* A copy file's text as its value; blank text is an empty file. `file` is the
|
|
76
|
+
* name an error uses.
|
|
77
|
+
*/ export let parse = (text, file = 'copy.json')=>{
|
|
78
|
+
if (!text.trim()) return {};
|
|
79
|
+
let value;
|
|
80
|
+
try {
|
|
81
|
+
value = JSON.parse(text);
|
|
82
|
+
} catch (e) {
|
|
83
|
+
throw new Error(`${file} is not JSON: ${e.message}`);
|
|
84
|
+
}
|
|
85
|
+
return check(value, '', file);
|
|
86
|
+
};
|
|
87
|
+
let held = (value)=>typeof value == 'string' || list(value);
|
|
88
|
+
let find = (copy, key)=>{
|
|
89
|
+
if (Object.hasOwn(copy, key) && held(copy[key])) {
|
|
90
|
+
return {
|
|
91
|
+
at: copy,
|
|
92
|
+
name: key
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
let parts = key.split('.');
|
|
96
|
+
for(let n = parts.length - 1; n > 0; n--){
|
|
97
|
+
let prefix = parts.slice(0, n).join('.');
|
|
98
|
+
let inner = Object.hasOwn(copy, prefix) ? copy[prefix] : undefined;
|
|
99
|
+
let spot = object(inner) && find(inner, parts.slice(n).join('.'));
|
|
100
|
+
if (spot) return spot;
|
|
101
|
+
}
|
|
102
|
+
return undefined;
|
|
103
|
+
};
|
|
104
|
+
/** What `key`, a dotted path through nested objects, holds: a snippet or a
|
|
105
|
+
* list. */ export let valueAt = (copy, key)=>{
|
|
106
|
+
let spot = find(copy, key);
|
|
107
|
+
return spot && spot.at[spot.name];
|
|
108
|
+
};
|
|
109
|
+
/** The snippet at `key`; a list there is not one. */ export let read = (copy, key)=>{
|
|
110
|
+
let value = valueAt(copy, key);
|
|
111
|
+
return typeof value == 'string' ? value : undefined;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* The list at `key`: its items' snippets, in order. A list is a JSON array
|
|
115
|
+
* of snippets, so its items can be added, removed and moved.
|
|
116
|
+
*
|
|
117
|
+
* ```ts
|
|
118
|
+
* items({ always: { items: ['Members', 'Visitors'] } }, 'always.items')
|
|
119
|
+
* // ['Members', 'Visitors']
|
|
120
|
+
* ```
|
|
121
|
+
*/ export let items = (copy, key)=>{
|
|
122
|
+
let value = valueAt(copy, key);
|
|
123
|
+
return Array.isArray(value) ? value : undefined;
|
|
124
|
+
};
|
|
125
|
+
/** Whether two values are the same snippet, or the same list. */ export let same = (a, b)=>JSON.stringify(a) == JSON.stringify(b);
|
|
126
|
+
/** Every snippet's and list's key, in the file's order. */ export let keys = (copy)=>Object.entries(copy).flatMap(([key, value])=>held(value) ? [
|
|
127
|
+
key
|
|
128
|
+
] : keys(value).map((inner)=>`${key}.${inner}`));
|
|
129
|
+
// `%{name}` is a slot; `%%{name}` is the text `%{name}`.
|
|
130
|
+
let SLOT = /(%?)%\{([\w-]+)\}/g;
|
|
131
|
+
/**
|
|
132
|
+
* A snippet's text with its slots filled: the parts in order, text between
|
|
133
|
+
* the values given. A slot given no value stays as its `%{name}`, so a missing
|
|
134
|
+
* part shows by name rather than as a gap.
|
|
135
|
+
*
|
|
136
|
+
* ```ts
|
|
137
|
+
* fill('Tax is included. %{plans}.', { plans: { a: '/pricing' } })
|
|
138
|
+
* // ['Tax is included. ', { a: '/pricing' }, '.']
|
|
139
|
+
* ```
|
|
140
|
+
*/ export let fill = (value, values = {})=>{
|
|
141
|
+
let parts = [];
|
|
142
|
+
let text = (s)=>{
|
|
143
|
+
if (!s) return;
|
|
144
|
+
let last = parts.length - 1;
|
|
145
|
+
if (typeof parts[last] == 'string') parts[last] += s;
|
|
146
|
+
else parts.push(s);
|
|
147
|
+
};
|
|
148
|
+
let from = 0;
|
|
149
|
+
for (let match of value.matchAll(SLOT)){
|
|
150
|
+
let [whole, escape, name] = match;
|
|
151
|
+
text(value.slice(from, match.index));
|
|
152
|
+
from = match.index + whole.length;
|
|
153
|
+
if (escape || !Object.hasOwn(values, name)) text(whole.slice(escape.length));
|
|
154
|
+
else parts.push(values[name]);
|
|
155
|
+
}
|
|
156
|
+
text(value.slice(from));
|
|
157
|
+
return parts;
|
|
158
|
+
};
|
|
159
|
+
/**
|
|
160
|
+
* A snippet from its parts in order, as `fill` gives them: text, which reads
|
|
161
|
+
* as itself (a `%{name}` in it is escaped), and each slot by its name. A page
|
|
162
|
+
* edited where it stands reads its snippet back this way.
|
|
163
|
+
*
|
|
164
|
+
* ```ts
|
|
165
|
+
* import { assertEquals } from '@std/assert'
|
|
166
|
+
* import { fill, join } from '@yaks/copy'
|
|
167
|
+
*
|
|
168
|
+
* let value = 'Tax is included. %{plans}.'
|
|
169
|
+
* assertEquals(join(['Tax is included. ', { slot: 'plans' }, '.']), value)
|
|
170
|
+
* assertEquals(fill(join(['100%{x}'])), ['100%{x}'])
|
|
171
|
+
* ```
|
|
172
|
+
*/ export let join = (parts)=>parts.map((part)=>typeof part == 'string' ? part.replace(/%\{([\w-]+)\}/g, '%%{$1}') : `%{${part.slot}}`).join('');
|
|
173
|
+
/** The slots a snippet names, in order. */ export let slots = (value)=>[
|
|
174
|
+
...value.matchAll(SLOT)
|
|
175
|
+
].filter(([, escape])=>!escape).map(([, , name])=>name);
|
|
176
|
+
/**
|
|
177
|
+
* A copy file's text, one snippet per line, so a text merge of two edits works
|
|
178
|
+
* by key. `indent` is what a level is indented by.
|
|
179
|
+
*/ export let format = (copy, indent = ' ')=>JSON.stringify(copy, null, indent) + '\n';
|
|
180
|
+
// A key not yet in the file goes into the deepest object a leading part of it
|
|
181
|
+
// names, so `hero.kicker` joins `{"hero": {…}}` and a key nothing names is
|
|
182
|
+
// added at the end of the file as it is written.
|
|
183
|
+
let place = (copy, key)=>{
|
|
184
|
+
let parts = key.split('.');
|
|
185
|
+
for(let n = parts.length - 1; n > 0; n--){
|
|
186
|
+
let prefix = parts.slice(0, n).join('.');
|
|
187
|
+
let inner = Object.hasOwn(copy, prefix) ? copy[prefix] : undefined;
|
|
188
|
+
if (object(inner)) return place(inner, parts.slice(n).join('.'));
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
at: copy,
|
|
192
|
+
name: key
|
|
193
|
+
};
|
|
194
|
+
};
|
|
195
|
+
/**
|
|
196
|
+
* A copy file's text with `changes` set by key, a snippet or a whole list. A
|
|
197
|
+
* key already there keeps its place; a new one is added at the end of the
|
|
198
|
+
* object its key leads into. The file's indentation is kept, and each snippet
|
|
199
|
+
* and list item stays on its own line.
|
|
200
|
+
*/ export let set = (text, changes, file = 'copy.json')=>format(setting(parse(text, file), changes), /\n([ \t]+)"/.exec(text)?.[1]);
|
|
201
|
+
/** A copy with changed values placed under their existing keys. */ export let withCopy = (copy, changes)=>setting(structuredClone(copy), changes);
|
|
202
|
+
let setting = (copy, changes)=>{
|
|
203
|
+
for (let [key, value] of Object.entries(changes)){
|
|
204
|
+
let { at, name } = find(copy, key) ?? place(copy, key);
|
|
205
|
+
Object.defineProperty(at, name, {
|
|
206
|
+
value,
|
|
207
|
+
writable: true,
|
|
208
|
+
enumerable: true,
|
|
209
|
+
configurable: true
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
return copy;
|
|
213
|
+
};
|
|
214
|
+
/**
|
|
215
|
+
* A copy file's text with each snippet another copy of it changed laid over
|
|
216
|
+
* it by key: `ours` is the file, `theirs` the other copy (an app's live
|
|
217
|
+
* file, edited on its page), and `base` what both last agreed on. A key
|
|
218
|
+
* `theirs` holds as `base` did is left as `ours` has it, so an edit or removal
|
|
219
|
+
* made only in `ours` stands; any other key `theirs` holds is set to its
|
|
220
|
+
* text, so an edit made in `theirs` wins, even where `ours` changed it too.
|
|
221
|
+
* With no base, every key `theirs` holds is its own.
|
|
222
|
+
*
|
|
223
|
+
* ```ts
|
|
224
|
+
* let base = '{\n "a": "one",\n "b": "two"\n}\n'
|
|
225
|
+
* let ours = '{\n "a": "uno",\n "b": "two",\n "c": "three"\n}\n'
|
|
226
|
+
* merge(ours, '{\n "a": "one",\n "b": "dos"\n}\n', base)
|
|
227
|
+
* // '{\n "a": "uno",\n "b": "dos",\n "c": "three"\n}\n'
|
|
228
|
+
* ```
|
|
229
|
+
*/ export let merge = (ours, theirs, base = '', file = 'copy.json')=>{
|
|
230
|
+
let [o, t, b] = [
|
|
231
|
+
ours,
|
|
232
|
+
theirs,
|
|
233
|
+
base
|
|
234
|
+
].map((text)=>parse(text, file));
|
|
235
|
+
let changes = Object.fromEntries(keys(t).flatMap((key)=>{
|
|
236
|
+
let value = valueAt(t, key);
|
|
237
|
+
return same(value, valueAt(b, key)) || same(value, valueAt(o, key)) ? [] : [
|
|
238
|
+
[
|
|
239
|
+
key,
|
|
240
|
+
value
|
|
241
|
+
]
|
|
242
|
+
];
|
|
243
|
+
}));
|
|
244
|
+
return Object.keys(changes).length ? set(ours, changes, file) : ours;
|
|
245
|
+
};
|
|
246
|
+
export * from "./locales.js";
|
|
247
|
+
export * from "./translation.js";
|
|
248
|
+
//# sourceMappingURL=mod.js.map
|
package/mod.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;CAiBC,GAED;oBACoB,GAMpB,mDAAmD,GACnD,OAAO,IAAI,QASP;EACF,MAAM;EACN,MAAM;EACN,MAAM;EACN,MAAM,CAAC,OAAS,CAAC,UAAU,EAAE,MAAM;AACrC,EAAC;AAkBD;;;;;;;;CAQC,GACD,OAAO,IAAI,QAAQ,CAAC;EAClB,IAAI,QAAQ,OAAO,QAAQ,IAAI,aAC3B;OAAI;GAAwD,CAC3D,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,GAAK;MAAC;MAAM;KAAM,IACvC,OAAO,OAAO,CAAC;EACnB,IAAI,QAAe;IAAE,OAA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ACZ,cAAc;IAChB;EACF;EACA,OAAO;AACT;AAEA;;;;;;;;;;;;;;;CAeC,GACD,OAAO,IAAI,QAAQ,CACjB,MACA,QACA,OAAO,EAAE,EACT,OAAO,WAAW;EAElB,IAAI,CAAC,GAAG,GAAG,EAAE,GAAG;IAAC;IAAM;IAAQ;GAAK,CAAC,GAAG,CAAC,CAAC,OAAS,MAAM,MAAM;EAC/D,IAAI,UAAU,OAAO,WAAW,CAC9B,KAAK,GAAG,OAAO,CAAC,CAAC;IACf,IAAI,QAAQ,QAAQ,GAAG;IACvB,OAAO,KAAK,OAAO,QAAQ,GAAG,SAAS,KAAK,OAAO,QAAQ,GAAG,QAC1D,EAAE,GACF;MAAC;QAAC;QAAK;OAAM;KAAC;EACpB;EAEF,OAAO,OAAO,IAAI,CAAC,SAAS,MAAM,GAAG,IAAI,MAAM,SAAS,QAAQ;AAClE,EAAC;AAED,6BAA4B;AAC5B,iCAAgC"}
|
package/mod.ts
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/copy — an app's copy: the words its pages show, kept in `copy.json` at
|
|
3
|
+
* the app's root as keyed snippets, apart from the markup that shows them.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { fill, parse, read, set } from '@yaks/copy'
|
|
7
|
+
*
|
|
8
|
+
* let copy = parse('{\n "hero": {\n "title": "Build, host and share web apps"\n }\n}\n')
|
|
9
|
+
* read(copy, 'hero.title') // 'Build, host and share web apps'
|
|
10
|
+
* fill('Your first space is %{price}.', { price: 'free' }) // ['Your first space is ', 'free', '.']
|
|
11
|
+
* set('{}', { 'hero.kicker': 'yaks.app' }) // '{\n "hero.kicker": "yaks.app"\n}\n'
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* Pure logic and data: it takes and returns strings and values, and reads no
|
|
15
|
+
* file and touches no page.
|
|
16
|
+
*
|
|
17
|
+
* @module
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** A copy file's value: snippets and lists of them by key, and nested
|
|
21
|
+
* objects of more. */
|
|
22
|
+
export type Copy = { [key: string]: string | string[] | Copy }
|
|
23
|
+
|
|
24
|
+
/** What a key holds: a snippet, or a list's snippets in order. */
|
|
25
|
+
export type Value = string | string[]
|
|
26
|
+
|
|
27
|
+
/** The attributes an element names its copy with. */
|
|
28
|
+
export let names: {
|
|
29
|
+
/** The key of the element's text. */
|
|
30
|
+
text: 'data-copy'
|
|
31
|
+
/** The slot of its parent's snippet this element fills. */
|
|
32
|
+
slot: 'data-copy-slot'
|
|
33
|
+
/** The key of the list whose items are the element's children. */
|
|
34
|
+
list: 'data-copy-list'
|
|
35
|
+
/** The attribute naming the key of the element's attribute `name`. */
|
|
36
|
+
attr: (name: string) => string
|
|
37
|
+
} = {
|
|
38
|
+
text: 'data-copy',
|
|
39
|
+
slot: 'data-copy-slot',
|
|
40
|
+
list: 'data-copy-list',
|
|
41
|
+
attr: (name) => `data-copy-${name}`,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** What an element's attributes say about its copy. */
|
|
45
|
+
export type Marks = {
|
|
46
|
+
/** The key of its text. */
|
|
47
|
+
text?: string
|
|
48
|
+
/** The slot it fills in its parent's snippet. */
|
|
49
|
+
slot?: string
|
|
50
|
+
/** The key of the list its children are the items of. */
|
|
51
|
+
list?: string
|
|
52
|
+
/** The key of each attribute's value, by attribute. */
|
|
53
|
+
attrs: Record<string, string>
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
type Attributes =
|
|
57
|
+
| Iterable<{ name: string; value: string }>
|
|
58
|
+
| Record<string, string>
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The copy an element's attributes name, from a DOM element's `attributes` or
|
|
62
|
+
* a plain object of them.
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* marks({ 'data-copy': 'cost.plans', 'data-copy-slot': 'plans', href: '/pricing' })
|
|
66
|
+
* // { text: 'cost.plans', slot: 'plans', attrs: {} }
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export let marks = (attributes: Attributes): Marks => {
|
|
70
|
+
let pairs = Symbol.iterator in attributes
|
|
71
|
+
? [...attributes as Iterable<{ name: string; value: string }>]
|
|
72
|
+
.map(({ name, value }) => [name, value])
|
|
73
|
+
: Object.entries(attributes)
|
|
74
|
+
let found: Marks = { attrs: {} }
|
|
75
|
+
let prefix = names.attr('')
|
|
76
|
+
for (let [name, value] of pairs) {
|
|
77
|
+
if (name == names.text) found.text = value
|
|
78
|
+
else if (name == names.slot) found.slot = value
|
|
79
|
+
else if (name == names.list) found.list = value
|
|
80
|
+
else if (
|
|
81
|
+
name.startsWith(prefix) &&
|
|
82
|
+
!['data-copy-languages', 'data-copy-suggest'].includes(name)
|
|
83
|
+
) {
|
|
84
|
+
found.attrs[name.slice(prefix.length)] = value
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return found
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
let object = (value: unknown): value is Copy =>
|
|
91
|
+
typeof value == 'object' && value != null && !Array.isArray(value)
|
|
92
|
+
|
|
93
|
+
let list = (value: unknown): value is string[] =>
|
|
94
|
+
Array.isArray(value) && value.every((item) => typeof item == 'string')
|
|
95
|
+
|
|
96
|
+
let check = (value: unknown, path: string, file: string): Copy => {
|
|
97
|
+
if (!object(value)) {
|
|
98
|
+
throw new Error(
|
|
99
|
+
`${file}: ${path || 'the file'} is not an object of snippets`,
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
for (let [key, inner] of Object.entries(value)) {
|
|
103
|
+
let at = path ? `${path}.${key}` : key
|
|
104
|
+
if (Array.isArray(inner) && !list(inner)) {
|
|
105
|
+
throw new Error(`${file}: ${at} is a list of something other than text`)
|
|
106
|
+
}
|
|
107
|
+
if (typeof inner != 'string' && !Array.isArray(inner)) {
|
|
108
|
+
check(inner, at, file)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return value
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* A copy file's text as its value; blank text is an empty file. `file` is the
|
|
116
|
+
* name an error uses.
|
|
117
|
+
*/
|
|
118
|
+
export let parse = (text: string, file = 'copy.json'): Copy => {
|
|
119
|
+
if (!text.trim()) return {}
|
|
120
|
+
let value: unknown
|
|
121
|
+
try {
|
|
122
|
+
value = JSON.parse(text)
|
|
123
|
+
} catch (e) {
|
|
124
|
+
throw new Error(`${file} is not JSON: ${(e as Error).message}`)
|
|
125
|
+
}
|
|
126
|
+
return check(value, '', file)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Where a key lives: the object holding it and its name there. A key is
|
|
130
|
+
// looked for whole first, then under each object a leading part of it names,
|
|
131
|
+
// the longest part first, so `"hero.title"` and `{"hero": {"title"}}` both
|
|
132
|
+
// answer `hero.title`.
|
|
133
|
+
type Spot = { at: Copy; name: string }
|
|
134
|
+
|
|
135
|
+
let held = (value: unknown): value is Value =>
|
|
136
|
+
typeof value == 'string' || list(value)
|
|
137
|
+
|
|
138
|
+
let find = (copy: Copy, key: string): Spot | undefined => {
|
|
139
|
+
if (Object.hasOwn(copy, key) && held(copy[key])) {
|
|
140
|
+
return { at: copy, name: key }
|
|
141
|
+
}
|
|
142
|
+
let parts = key.split('.')
|
|
143
|
+
for (let n = parts.length - 1; n > 0; n--) {
|
|
144
|
+
let prefix = parts.slice(0, n).join('.')
|
|
145
|
+
let inner = Object.hasOwn(copy, prefix) ? copy[prefix] : undefined
|
|
146
|
+
let spot = object(inner) && find(inner, parts.slice(n).join('.'))
|
|
147
|
+
if (spot) return spot
|
|
148
|
+
}
|
|
149
|
+
return undefined
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** What `key`, a dotted path through nested objects, holds: a snippet or a
|
|
153
|
+
* list. */
|
|
154
|
+
export let valueAt = (copy: Copy, key: string): Value | undefined => {
|
|
155
|
+
let spot = find(copy, key)
|
|
156
|
+
return spot && spot.at[spot.name] as Value
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The snippet at `key`; a list there is not one. */
|
|
160
|
+
export let read = (copy: Copy, key: string): string | undefined => {
|
|
161
|
+
let value = valueAt(copy, key)
|
|
162
|
+
return typeof value == 'string' ? value : undefined
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* The list at `key`: its items' snippets, in order. A list is a JSON array
|
|
167
|
+
* of snippets, so its items can be added, removed and moved.
|
|
168
|
+
*
|
|
169
|
+
* ```ts
|
|
170
|
+
* items({ always: { items: ['Members', 'Visitors'] } }, 'always.items')
|
|
171
|
+
* // ['Members', 'Visitors']
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
export let items = (copy: Copy, key: string): string[] | undefined => {
|
|
175
|
+
let value = valueAt(copy, key)
|
|
176
|
+
return Array.isArray(value) ? value : undefined
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Whether two values are the same snippet, or the same list. */
|
|
180
|
+
export let same = (a: Value | undefined, b: Value | undefined): boolean =>
|
|
181
|
+
JSON.stringify(a) == JSON.stringify(b)
|
|
182
|
+
|
|
183
|
+
/** Every snippet's and list's key, in the file's order. */
|
|
184
|
+
export let keys = (copy: Copy): string[] =>
|
|
185
|
+
Object.entries(copy).flatMap(([key, value]) =>
|
|
186
|
+
held(value) ? [key] : keys(value).map((inner) => `${key}.${inner}`)
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
// `%{name}` is a slot; `%%{name}` is the text `%{name}`.
|
|
190
|
+
let SLOT = /(%?)%\{([\w-]+)\}/g
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* A snippet's text with its slots filled: the parts in order, text between
|
|
194
|
+
* the values given. A slot given no value stays as its `%{name}`, so a missing
|
|
195
|
+
* part shows by name rather than as a gap.
|
|
196
|
+
*
|
|
197
|
+
* ```ts
|
|
198
|
+
* fill('Tax is included. %{plans}.', { plans: { a: '/pricing' } })
|
|
199
|
+
* // ['Tax is included. ', { a: '/pricing' }, '.']
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
export let fill = <T>(
|
|
203
|
+
value: string,
|
|
204
|
+
values: Record<string, T> = {},
|
|
205
|
+
): Array<string | T> => {
|
|
206
|
+
let parts: Array<string | T> = []
|
|
207
|
+
let text = (s: string) => {
|
|
208
|
+
if (!s) return
|
|
209
|
+
let last = parts.length - 1
|
|
210
|
+
if (typeof parts[last] == 'string') parts[last] += s
|
|
211
|
+
else parts.push(s)
|
|
212
|
+
}
|
|
213
|
+
let from = 0
|
|
214
|
+
for (let match of value.matchAll(SLOT)) {
|
|
215
|
+
let [whole, escape, name] = match
|
|
216
|
+
text(value.slice(from, match.index))
|
|
217
|
+
from = match.index + whole.length
|
|
218
|
+
if (escape || !Object.hasOwn(values, name)) text(whole.slice(escape.length))
|
|
219
|
+
else parts.push(values[name])
|
|
220
|
+
}
|
|
221
|
+
text(value.slice(from))
|
|
222
|
+
return parts
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* A snippet from its parts in order, as `fill` gives them: text, which reads
|
|
227
|
+
* as itself (a `%{name}` in it is escaped), and each slot by its name. A page
|
|
228
|
+
* edited where it stands reads its snippet back this way.
|
|
229
|
+
*
|
|
230
|
+
* ```ts
|
|
231
|
+
* import { assertEquals } from '@std/assert'
|
|
232
|
+
* import { fill, join } from '@yaks/copy'
|
|
233
|
+
*
|
|
234
|
+
* let value = 'Tax is included. %{plans}.'
|
|
235
|
+
* assertEquals(join(['Tax is included. ', { slot: 'plans' }, '.']), value)
|
|
236
|
+
* assertEquals(fill(join(['100%{x}'])), ['100%{x}'])
|
|
237
|
+
* ```
|
|
238
|
+
*/
|
|
239
|
+
export let join = (parts: Array<string | { slot: string }>): string =>
|
|
240
|
+
parts.map((part) =>
|
|
241
|
+
typeof part == 'string'
|
|
242
|
+
? part.replace(/%\{([\w-]+)\}/g, '%%{$1}')
|
|
243
|
+
: `%{${part.slot}}`
|
|
244
|
+
).join('')
|
|
245
|
+
|
|
246
|
+
/** The slots a snippet names, in order. */
|
|
247
|
+
export let slots = (value: string): string[] =>
|
|
248
|
+
[...value.matchAll(SLOT)].filter(([, escape]) => !escape).map(([, , name]) =>
|
|
249
|
+
name
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* A copy file's text, one snippet per line, so a text merge of two edits works
|
|
254
|
+
* by key. `indent` is what a level is indented by.
|
|
255
|
+
*/
|
|
256
|
+
export let format = (copy: Copy, indent = ' '): string =>
|
|
257
|
+
JSON.stringify(copy, null, indent) + '\n'
|
|
258
|
+
|
|
259
|
+
// A key not yet in the file goes into the deepest object a leading part of it
|
|
260
|
+
// names, so `hero.kicker` joins `{"hero": {…}}` and a key nothing names is
|
|
261
|
+
// added at the end of the file as it is written.
|
|
262
|
+
let place = (copy: Copy, key: string): Spot => {
|
|
263
|
+
let parts = key.split('.')
|
|
264
|
+
for (let n = parts.length - 1; n > 0; n--) {
|
|
265
|
+
let prefix = parts.slice(0, n).join('.')
|
|
266
|
+
let inner = Object.hasOwn(copy, prefix) ? copy[prefix] : undefined
|
|
267
|
+
if (object(inner)) return place(inner, parts.slice(n).join('.'))
|
|
268
|
+
}
|
|
269
|
+
return { at: copy, name: key }
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* A copy file's text with `changes` set by key, a snippet or a whole list. A
|
|
274
|
+
* key already there keeps its place; a new one is added at the end of the
|
|
275
|
+
* object its key leads into. The file's indentation is kept, and each snippet
|
|
276
|
+
* and list item stays on its own line.
|
|
277
|
+
*/
|
|
278
|
+
export let set = (
|
|
279
|
+
text: string,
|
|
280
|
+
changes: Record<string, Value>,
|
|
281
|
+
file = 'copy.json',
|
|
282
|
+
): string =>
|
|
283
|
+
format(setting(parse(text, file), changes), /\n([ \t]+)"/.exec(text)?.[1])
|
|
284
|
+
|
|
285
|
+
/** A copy with changed values placed under their existing keys. */
|
|
286
|
+
export let withCopy = (copy: Copy, changes: Record<string, Value>): Copy =>
|
|
287
|
+
setting(structuredClone(copy), changes)
|
|
288
|
+
|
|
289
|
+
let setting = (copy: Copy, changes: Record<string, Value>) => {
|
|
290
|
+
for (let [key, value] of Object.entries(changes)) {
|
|
291
|
+
let { at, name } = find(copy, key) ?? place(copy, key)
|
|
292
|
+
Object.defineProperty(at, name, {
|
|
293
|
+
value,
|
|
294
|
+
writable: true,
|
|
295
|
+
enumerable: true,
|
|
296
|
+
configurable: true,
|
|
297
|
+
})
|
|
298
|
+
}
|
|
299
|
+
return copy
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* A copy file's text with each snippet another copy of it changed laid over
|
|
304
|
+
* it by key: `ours` is the file, `theirs` the other copy (an app's live
|
|
305
|
+
* file, edited on its page), and `base` what both last agreed on. A key
|
|
306
|
+
* `theirs` holds as `base` did is left as `ours` has it, so an edit or removal
|
|
307
|
+
* made only in `ours` stands; any other key `theirs` holds is set to its
|
|
308
|
+
* text, so an edit made in `theirs` wins, even where `ours` changed it too.
|
|
309
|
+
* With no base, every key `theirs` holds is its own.
|
|
310
|
+
*
|
|
311
|
+
* ```ts
|
|
312
|
+
* let base = '{\n "a": "one",\n "b": "two"\n}\n'
|
|
313
|
+
* let ours = '{\n "a": "uno",\n "b": "two",\n "c": "three"\n}\n'
|
|
314
|
+
* merge(ours, '{\n "a": "one",\n "b": "dos"\n}\n', base)
|
|
315
|
+
* // '{\n "a": "uno",\n "b": "dos",\n "c": "three"\n}\n'
|
|
316
|
+
* ```
|
|
317
|
+
*/
|
|
318
|
+
export let merge = (
|
|
319
|
+
ours: string,
|
|
320
|
+
theirs: string,
|
|
321
|
+
base = '',
|
|
322
|
+
file = 'copy.json',
|
|
323
|
+
): string => {
|
|
324
|
+
let [o, t, b] = [ours, theirs, base].map((text) => parse(text, file))
|
|
325
|
+
let changes = Object.fromEntries(
|
|
326
|
+
keys(t).flatMap((key) => {
|
|
327
|
+
let value = valueAt(t, key)!
|
|
328
|
+
return same(value, valueAt(b, key)) || same(value, valueAt(o, key))
|
|
329
|
+
? []
|
|
330
|
+
: [[key, value]]
|
|
331
|
+
}),
|
|
332
|
+
)
|
|
333
|
+
return Object.keys(changes).length ? set(ours, changes, file) : ours
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export * from './locales.js'
|
|
337
|
+
export * from './translation.js'
|
package/package.json
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaks/copy",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.36",
|
|
4
|
+
"homepage": "https://jsr.io/@yaks/copy",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./_dist/mod.d.ts",
|
|
9
|
+
"default": "./mod.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"license": "Apache-2.0",
|
|
13
|
+
"dependencies": {},
|
|
14
|
+
"description": "An app's copy as a file of keyed snippets: read by key, filled by slot, set by key, one snippet per line.",
|
|
15
|
+
"repository": "https://github.com/yak-sh/yak.git"
|
|
16
|
+
}
|