deno.land / std@0.224.0 / ini / mod.ts

نووسراو ببینە
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible./** * {@linkcode parse} and {@linkcode stringify} for handling * {@link https://en.wikipedia.org/wiki/INI_file | INI} encoded data, such as the * {@link https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s03.html | Desktop Entry specification}. * Values are parsed as strings by default to preserve data parity from the original. * Customization is possible in the form of reviver/replacer functions like those in `JSON.parse` and `JSON.stringify`. * Nested sections, repeated key names within a section, and key/value arrays are not supported, * but will be preserved when using {@linkcode IniMap}. Multi-line values are not supported and will throw a syntax error. * White space padding and lines starting with '#', ';', or '//' will be treated as comments. * * @example * ```ts * import * as ini from "https://deno.land/std@$STD_VERSION/ini/mod.ts"; * const iniFile = `# Example configuration file * Global Key=Some data here * * [Section #1] * Section Value=42 * Section Date=1977-05-25 * `; * const parsed = ini.parse(iniFile, { * reviver: (key, value, section) => { * if (section === "Section #1") { * if (key === "Section Value") return Number(value); * if (key === "Section Date") return new Date(value); * } * return value; * }, * }); * console.log(parsed); * * // => * // { * // "Global Key": "Some data here", * // "Section #1": { "Section Value": 42, "Section Date": 1977-05-25T00:00:00.000Z } * // } * * const text = ini.stringify(parsed, { * replacer: (key, value, section) => { * if (section === "Section #1" && key === "Section Date") { * return (value as Date).toISOString().split("T")[0]; * } * return value; * }, * }); * console.log(text); * * // => * // Global Key=Some data here * // [Section #1] * // Section Value=42 * // Section Date=1977-05-25 * ``` * * Optionally, {@linkcode IniMap} may be used for finer INI handling. Using this class will permit preserving * comments, accessing values like a map, iterating over key/value/section entries, and more. * * @example * ```ts * import { IniMap } from "https://deno.land/std@$STD_VERSION/ini/mod.ts"; * const ini = new IniMap(); * ini.set("section1", "keyA", 100) * console.log(ini.toString()) * * // => * // [section1] * // keyA=100 * * ini.set('keyA', 25) * console.log(ini.toObject()) * * // => * // { * // keyA: 25, * // section1: { * // keyA: 100 * // } * // } * ``` * * The reviver and replacer APIs can be used to extend the behavior of IniMap, such as adding support * for duplicate keys as if they were arrays of values. * * @example * ```ts * import { IniMap } from "https://deno.land/std@$STD_VERSION/ini/mod.ts"; * const iniFile = `# Example of key/value arrays * [section1] * key1=This key * key1=is non-standard * key1=but can be captured! * `; * const ini = new IniMap({ assignment: "=", deduplicate: true }); * ini.parse(iniFile, (key, value, section) => { * if (section) { * if (ini.has(section, key)) { * const exists = ini.get(section, key); * if (Array.isArray(exists)) { * exists.push(value); * return exists; * } else { * return [exists, value]; * } * } * } * return value; * }); * console.log(ini.get("section1", "key1")); * * // => [ "This key", "is non-standard", "but can be captured!" ] * * const result = ini.toString((key, value) => { * if (Array.isArray(value)) { * return value.join( * `${ini.formatting.lineBreak}${key}${ini.formatting.assignment}`, * ); * } * return value; * }); * console.log(result === iniFile); * * // => true * ``` * * @module */
export * from "./ini_map.ts";export * from "./parse.ts";export * from "./stringify.ts";
std

Version Info

Tagged at
3 weeks ago