@office-open/core 0.3.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,400 @@
1
+ //#region src/values.d.ts
2
+ /**
3
+ * Runtime validation and type conversion functions for OOXML specification values.
4
+ *
5
+ * This module provides runtime checks and cleanup for value types in the OOXML spec
6
+ * that aren't easily expressed through the TypeScript type system alone. These
7
+ * validators help prevent silent failures and corrupted documents by enforcing
8
+ * spec-compliant values at runtime.
9
+ *
10
+ * @module
11
+ */
12
+ /**
13
+ * A measurement value with optional sign and unit suffix.
14
+ *
15
+ * Supports units: mm (millimeters), cm (centimeters), in (inches),
16
+ * pt (points), pc (picas), pi (picas).
17
+ *
18
+ * Pattern: `-?[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)`
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * const measure: UniversalMeasure = "10.5mm";
23
+ * const negative: UniversalMeasure = "-5pt";
24
+ * ```
25
+ */
26
+ type UniversalMeasure = `${"-" | ""}${number}${"mm" | "cm" | "in" | "pt" | "pc" | "pi"}`;
27
+ /**
28
+ * A positive measurement value with unit suffix.
29
+ *
30
+ * Same as UniversalMeasure but restricted to positive values only.
31
+ *
32
+ * Reference: ST_PositiveUniversalMeasure in OOXML specification
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * const measure: PositiveUniversalMeasure = "10.5mm";
37
+ * ```
38
+ */
39
+ type PositiveUniversalMeasure = `${number}${"mm" | "cm" | "in" | "pt" | "pc" | "pi"}`;
40
+ /**
41
+ * A percentage value with optional sign.
42
+ *
43
+ * Pattern: `-?[0-9]+(\.[0-9]+)?%`
44
+ *
45
+ * Reference: ST_Percentage in OOXML specification
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * const percent: Percentage = "50%";
50
+ * const negative: Percentage = "-10.5%";
51
+ * ```
52
+ */
53
+ type Percentage = `${"-" | ""}${number}%`;
54
+ /**
55
+ * A positive percentage value.
56
+ *
57
+ * Same as Percentage but restricted to positive values only.
58
+ *
59
+ * Reference: ST_PositivePercentage in OOXML specification
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * const percent: PositivePercentage = "50%";
64
+ * ```
65
+ */
66
+ type PositivePercentage = `${number}%`;
67
+ /**
68
+ * A relative measurement value using em or ex units.
69
+ *
70
+ * Used in VML text boxes for font-relative measurements.
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * const measure: RelativeMeasure = "2em";
75
+ * const negative: RelativeMeasure = "-0.5ex";
76
+ * ```
77
+ */
78
+ type RelativeMeasure = `${"-" | ""}${number}${"em" | "ex"}`;
79
+ /**
80
+ * Validates and converts a number to an integer (decimal number).
81
+ *
82
+ * Reference: ST_DecimalNumber in OOXML specification
83
+ *
84
+ * @param val - The number to validate and convert
85
+ * @returns The floored integer value
86
+ * @throws Error if the value is NaN
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * const num = decimalNumber(10.7); // Returns 10
91
+ * const negative = decimalNumber(-5.3); // Returns -5
92
+ * ```
93
+ */
94
+ declare const decimalNumber: (val: number) => number;
95
+ /**
96
+ * Validates and converts a number to a positive integer (unsigned decimal number).
97
+ *
98
+ * Reference: ST_UnsignedDecimalNumber in OOXML specification
99
+ *
100
+ * @param val - The number to validate and convert
101
+ * @returns The floored positive integer value
102
+ * @throws Error if the value is NaN or negative
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * const num = unsignedDecimalNumber(10.7); // Returns 10
107
+ * const invalid = unsignedDecimalNumber(-5); // Throws Error
108
+ * ```
109
+ */
110
+ declare const unsignedDecimalNumber: (val: number) => number;
111
+ /**
112
+ * Validates a long hexadecimal number (4 bytes / 8 characters).
113
+ *
114
+ * Reference: ST_LongHexNumber in OOXML specification
115
+ *
116
+ * @param val - The hexadecimal string to validate
117
+ * @returns The validated hexadecimal string
118
+ * @throws Error if the value is not a valid 8-character hex string
119
+ *
120
+ * @example
121
+ * ```typescript
122
+ * const hex = longHexNumber("ABCD1234"); // Valid
123
+ * ```
124
+ */
125
+ declare const longHexNumber: (val: string) => string;
126
+ /**
127
+ * Validates a short hexadecimal number (2 bytes / 4 characters).
128
+ *
129
+ * Reference: ST_ShortHexNumber in OOXML specification
130
+ *
131
+ * @param val - The hexadecimal string to validate
132
+ * @returns The validated hexadecimal string
133
+ * @throws Error if the value is not a valid 4-character hex string
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * const hex = shortHexNumber("AB12"); // Valid
138
+ * ```
139
+ */
140
+ declare const shortHexNumber: (val: string) => string;
141
+ /**
142
+ * Validates a single-byte hexadecimal number (1 byte / 2 characters).
143
+ *
144
+ * Reference: ST_UcharHexNumber in OOXML specification
145
+ *
146
+ * @param val - The hexadecimal string to validate
147
+ * @returns The validated hexadecimal string
148
+ * @throws Error if the value is not a valid 2-character hex string
149
+ *
150
+ * @example
151
+ * ```typescript
152
+ * const hex = uCharHexNumber("FF"); // Valid
153
+ * ```
154
+ */
155
+ declare const uCharHexNumber: (val: string) => string;
156
+ /**
157
+ * Normalizes a universal measure value by parsing and reformatting.
158
+ *
159
+ * Ensures the numeric portion is properly formatted while preserving the unit.
160
+ *
161
+ * Reference: ST_UniversalMeasure in OOXML specification
162
+ *
163
+ * @param val - The universal measure string to normalize
164
+ * @returns The normalized universal measure
165
+ *
166
+ * @example
167
+ * ```typescript
168
+ * const measure = universalMeasureValue("10.500mm"); // Returns "10.5mm"
169
+ * ```
170
+ */
171
+ declare const universalMeasureValue: (val: UniversalMeasure) => UniversalMeasure;
172
+ /**
173
+ * Validates and normalizes a positive universal measure value.
174
+ *
175
+ * Reference: ST_PositiveUniversalMeasure in OOXML specification
176
+ *
177
+ * @param val - The positive universal measure string to validate
178
+ * @returns The normalized positive universal measure
179
+ * @throws Error if the value is negative
180
+ *
181
+ * @example
182
+ * ```typescript
183
+ * const measure = positiveUniversalMeasureValue("10.5mm"); // Valid
184
+ * const invalid = positiveUniversalMeasureValue("-5mm"); // Throws Error
185
+ * ```
186
+ */
187
+ declare const positiveUniversalMeasureValue: (val: PositiveUniversalMeasure) => PositiveUniversalMeasure;
188
+ /**
189
+ * Validates and normalizes a hexadecimal color value.
190
+ *
191
+ * Accepts either "auto" or a 6-character RGB hex value (with or without # prefix).
192
+ * The # prefix is commonly used but technically invalid in OOXML, so it is stripped
193
+ * for strict compliance.
194
+ *
195
+ * Reference: ST_HexColor in OOXML specification
196
+ *
197
+ * @param val - The color value to validate ("auto" or hex color)
198
+ * @returns The normalized color value
199
+ * @throws Error if the hex color is invalid
200
+ *
201
+ * @example
202
+ * ```typescript
203
+ * const color1 = hexColorValue("auto"); // Returns "auto"
204
+ * const color2 = hexColorValue("FF0000"); // Returns "FF0000"
205
+ * const color3 = hexColorValue("#00FF00"); // Returns "00FF00" (# stripped)
206
+ * ```
207
+ */
208
+ declare const hexColorValue: (val: string) => string;
209
+ /**
210
+ * Validates a signed TWIP measurement value.
211
+ *
212
+ * Accepts either a universal measure string or a numeric TWIP value.
213
+ *
214
+ * Reference: ST_SignedTwipsMeasure in OOXML specification
215
+ *
216
+ * @param val - The measurement value (universal measure or number)
217
+ * @returns The normalized measurement value
218
+ *
219
+ * @example
220
+ * ```typescript
221
+ * const measure1 = signedTwipsMeasureValue("10mm");
222
+ * const measure2 = signedTwipsMeasureValue(1440); // 1 inch in TWIP
223
+ * ```
224
+ */
225
+ declare const signedTwipsMeasureValue: (val: UniversalMeasure | number) => UniversalMeasure | number;
226
+ /**
227
+ * Validates a half-point (HPS) measurement value.
228
+ *
229
+ * Accepts either a positive universal measure string or a positive number.
230
+ * HPS (half-points) are commonly used for font sizes.
231
+ *
232
+ * Reference: ST_HpsMeasure in OOXML specification
233
+ *
234
+ * @param val - The measurement value (positive universal measure or number)
235
+ * @returns The normalized measurement value
236
+ *
237
+ * @example
238
+ * ```typescript
239
+ * const fontSize1 = hpsMeasureValue("12pt");
240
+ * const fontSize2 = hpsMeasureValue(24); // 12pt in half-points
241
+ * ```
242
+ */
243
+ declare const hpsMeasureValue: (val: PositiveUniversalMeasure | number) => string | number;
244
+ /**
245
+ * Validates a signed half-point (HPS) measurement value.
246
+ *
247
+ * Accepts either a universal measure string or a numeric value.
248
+ *
249
+ * Reference: ST_SignedHpsMeasure in OOXML specification
250
+ *
251
+ * @param val - The measurement value (universal measure or number)
252
+ * @returns The normalized measurement value
253
+ *
254
+ * @example
255
+ * ```typescript
256
+ * const spacing1 = signedHpsMeasureValue("6pt");
257
+ * const spacing2 = signedHpsMeasureValue(-12); // Negative spacing
258
+ * ```
259
+ */
260
+ declare const signedHpsMeasureValue: (val: UniversalMeasure | number) => string | number;
261
+ /**
262
+ * Validates a positive TWIP measurement value.
263
+ *
264
+ * Accepts either a positive universal measure string or a positive number.
265
+ *
266
+ * Reference: ST_TwipsMeasure in OOXML specification
267
+ *
268
+ * @param val - The measurement value (positive universal measure or number)
269
+ * @returns The normalized measurement value
270
+ *
271
+ * @example
272
+ * ```typescript
273
+ * const width1 = twipsMeasureValue("25.4mm");
274
+ * const width2 = twipsMeasureValue(1440); // 1 inch in TWIP
275
+ * ```
276
+ */
277
+ declare const twipsMeasureValue: (val: PositiveUniversalMeasure | number) => PositiveUniversalMeasure | number;
278
+ /**
279
+ * Normalizes a percentage value by parsing and reformatting.
280
+ *
281
+ * Reference: ST_Percentage in OOXML specification
282
+ *
283
+ * @param val - The percentage string to normalize
284
+ * @returns The normalized percentage
285
+ *
286
+ * @example
287
+ * ```typescript
288
+ * const percent = percentageValue("50.000%"); // Returns "50%"
289
+ * ```
290
+ */
291
+ declare const percentageValue: (val: Percentage) => Percentage;
292
+ /**
293
+ * Validates a measurement value that can be expressed as a number, percentage, or universal measure.
294
+ *
295
+ * Reference: ST_MeasurementOrPercent in OOXML specification
296
+ *
297
+ * @param val - The measurement value (number, percentage, or universal measure)
298
+ * @returns The normalized measurement value
299
+ *
300
+ * @example
301
+ * ```typescript
302
+ * const measure1 = measurementOrPercentValue(100); // Unqualified number
303
+ * const measure2 = measurementOrPercentValue("50%"); // Percentage
304
+ * const measure3 = measurementOrPercentValue("10mm"); // Universal measure
305
+ * ```
306
+ */
307
+ declare const measurementOrPercentValue: (val: number | Percentage | UniversalMeasure) => number | UniversalMeasure | Percentage;
308
+ /**
309
+ * Validates an eighth-point measurement value.
310
+ *
311
+ * Eighth-points are used for fine-grained measurements in text formatting.
312
+ *
313
+ * Reference: ST_EighthPointMeasure in OOXML specification
314
+ *
315
+ * @param val - The measurement value in eighth-points
316
+ * @returns The validated positive integer value
317
+ *
318
+ * @example
319
+ * ```typescript
320
+ * const measure = eighthPointMeasureValue(16); // 2 points
321
+ * ```
322
+ */
323
+ declare const eighthPointMeasureValue: (val: number) => number;
324
+ /**
325
+ * Validates a point measurement value.
326
+ *
327
+ * Reference: ST_PointMeasure in OOXML specification
328
+ *
329
+ * @param val - The measurement value in points
330
+ * @returns The validated positive integer value
331
+ *
332
+ * @example
333
+ * ```typescript
334
+ * const fontSize = pointMeasureValue(12); // 12pt
335
+ * ```
336
+ */
337
+ declare const pointMeasureValue: (val: number) => number;
338
+ /**
339
+ * Converts a JavaScript Date object to an ISO 8601 date-time string.
340
+ *
341
+ * The format is CCYY-MM-DDThh:mm:ss.sssZ where T is a literal and Z indicates UTC.
342
+ * This matches the xsd:dateTime format required by OOXML.
343
+ *
344
+ * Reference: ST_DateTime in OOXML specification
345
+ *
346
+ * @param val - The Date object to convert
347
+ * @returns An ISO 8601 formatted date-time string
348
+ *
349
+ * @example
350
+ * ```typescript
351
+ * const now = new Date();
352
+ * const timestamp = dateTimeValue(now); // Returns "2024-01-15T10:30:00.000Z"
353
+ * ```
354
+ */
355
+ declare const dateTimeValue: (val: Date) => string;
356
+ /**
357
+ * Theme color values used throughout OOXML for referencing document theme colors.
358
+ *
359
+ * Reference: ST_ThemeColor in OOXML specification
360
+ *
361
+ * @publicApi
362
+ */
363
+ declare const ThemeColor: {
364
+ readonly DARK1: "dark1";
365
+ readonly LIGHT1: "light1";
366
+ readonly DARK2: "dark2";
367
+ readonly LIGHT2: "light2";
368
+ readonly ACCENT1: "accent1";
369
+ readonly ACCENT2: "accent2";
370
+ readonly ACCENT3: "accent3";
371
+ readonly ACCENT4: "accent4";
372
+ readonly ACCENT5: "accent5";
373
+ readonly ACCENT6: "accent6";
374
+ readonly HYPERLINK: "hyperlink";
375
+ readonly FOLLOWED_HYPERLINK: "followedHyperlink";
376
+ readonly NONE: "none";
377
+ readonly BACKGROUND1: "background1";
378
+ readonly TEXT1: "text1";
379
+ readonly BACKGROUND2: "background2";
380
+ readonly TEXT2: "text2";
381
+ };
382
+ /**
383
+ * Theme font values used for referencing document theme fonts.
384
+ *
385
+ * Reference: ST_Theme in OOXML specification
386
+ *
387
+ * @publicApi
388
+ */
389
+ declare const ThemeFont: {
390
+ readonly MAJOR_EAST_ASIA: "majorEastAsia";
391
+ readonly MAJOR_BIDI: "majorBidi";
392
+ readonly MAJOR_ASCII: "majorAscii";
393
+ readonly MAJOR_H_ANSI: "majorHAnsi";
394
+ readonly MINOR_EAST_ASIA: "minorEastAsia";
395
+ readonly MINOR_BIDI: "minorBidi";
396
+ readonly MINOR_ASCII: "minorAscii";
397
+ readonly MINOR_H_ANSI: "minorHAnsi";
398
+ };
399
+ //#endregion
400
+ export { universalMeasureValue as C, uCharHexNumber as S, positiveUniversalMeasureValue as _, ThemeColor as a, signedTwipsMeasureValue as b, dateTimeValue as c, hexColorValue as d, hpsMeasureValue as f, pointMeasureValue as g, percentageValue as h, RelativeMeasure as i, decimalNumber as l, measurementOrPercentValue as m, PositivePercentage as n, ThemeFont as o, longHexNumber as p, PositiveUniversalMeasure as r, UniversalMeasure as s, Percentage as t, eighthPointMeasureValue as u, shortHexNumber as v, unsignedDecimalNumber as w, twipsMeasureValue as x, signedHpsMeasureValue as y };