forked from sindresorhus/conf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test-d.ts
138 lines (119 loc) · 2.66 KB
/
index.test-d.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
134
135
136
137
138
import {expectType, expectError} from 'tsd';
import Conf = require('.');
type UnicornFoo = {
foo: string;
unicorn: boolean;
hello?: number;
};
const conf = new Conf<UnicornFoo>();
new Conf<UnicornFoo>({
defaults: {
foo: 'bar',
unicorn: false
}
});
new Conf<UnicornFoo>({configName: ''});
new Conf<UnicornFoo>({projectName: 'foo'});
new Conf<UnicornFoo>({cwd: ''});
new Conf<UnicornFoo>({encryptionKey: ''});
new Conf<UnicornFoo>({encryptionKey: new Buffer('')});
new Conf<UnicornFoo>({encryptionKey: new Uint8Array([1])});
new Conf<UnicornFoo>({encryptionKey: new DataView(new ArrayBuffer(2))});
new Conf<UnicornFoo>({fileExtension: '.foo'});
new Conf<UnicornFoo>({clearInvalidConfig: false});
new Conf<UnicornFoo>({serialize: value => 'foo'});
new Conf<UnicornFoo>({deserialize: string => ({foo: 'foo', unicorn: true})});
new Conf<UnicornFoo>({projectSuffix: 'foo'});
new Conf<UnicornFoo>({watch: true});
new Conf<UnicornFoo>({
schema: {
foo: {
type: 'string',
default: 'foobar'
},
unicorn: {
type: 'boolean'
},
hello: {
type: 'number'
}
}
});
expectError(
new Conf<UnicornFoo>({
schema: {
foo: {
type: 'nope'
},
unicorn: {
type: 'nope'
},
hello: {
type: 'nope'
}
}
})
);
conf.set('hello', 1);
conf.set('unicorn', false);
conf.set({foo: 'nope'});
expectType<string>(conf.get('foo'));
expectType<void>(conf.reset('foo', 'unicorn'));
expectType<string>(conf.get('foo', 'bar'));
conf.delete('foo');
expectType<boolean>(conf.has('foo'));
conf.clear();
const off = conf.onDidChange('foo', (oldValue, newValue) => {
expectType<UnicornFoo[keyof UnicornFoo]>(oldValue);
expectType<UnicornFoo[keyof UnicornFoo]>(newValue);
});
expectType<() => void>(off);
off();
conf.store = {
foo: 'bar',
unicorn: false
};
expectType<string>(conf.path);
expectType<number>(conf.size);
expectType<IterableIterator<[keyof UnicornFoo, UnicornFoo[keyof UnicornFoo]]>>(
conf[Symbol.iterator]()
);
for (const [key, value] of conf) {
expectType<keyof UnicornFoo>(key);
expectType<UnicornFoo[keyof UnicornFoo]>(value);
}
// -- Docs examples --
type StoreType = {
isRainbow: boolean,
unicorn?: string
}
const config = new Conf<StoreType>({
defaults: {
isRainbow: true
}
});
config.get('isRainbow');
//=> true
config.set('unicorn', '🦄');
console.log(config.get('unicorn'));
//=> '🦄'
config.delete('unicorn');
console.log(config.get('unicorn'));
//=> undefined
// --
// -- Migrations --
const store = new Conf({
migrations: {
'0.0.1': store => {
store.set('debug phase', true);
},
'1.0.0': store => {
store.delete('debug phase');
store.set('phase', '1.0');
},
'1.0.2': store => {
store.set('phase', '>1.0');
}
}
});
// --