type Alph = 'Q' | 'W' | 'E' | 'R' | 'T' | 'Y' | 'U' | 'I' | 'O' | 'P' | 'A' | 'S' | 'D' | 'F' | 'G' | 'H' | 'J' | 'K' | 'L' | 'X' | 'Z' | 'C' | 'V' | 'B' | 'N' | 'M'
type CamelToSnake<T extends string> = T extends `${infer S1}${Alph}${string}` ? T extends `${S1}${infer S2}` ? `${Lowercase<S1>}_${CamelToSnake<Uncapitalize<S2>>}` : T : T;
type AsdSnake = CamelToSnake<'asdAsdAsd'> // asd_asd_asd
type SnakeToCamel<T extends string> = T extends `${infer S1}_${infer S2}` ? `${Lowercase<S1>}${Capitalize<SnakeToCamel<S2>>}` : T;
type AsdCamel = SnakeToCamel<'asd_asd_asd'> // asdAsdAsd
type SnakePropToCamel<T extends PropertyKey> = T extends string ? SnakeToCamel<T> : T;
type CamelPropToSnake<T extends PropertyKey> = T extends string ? CamelToSnake<T> : T;
let camelToSnakeCase: <T extends PropertyKey>(str: T) => CamelPropToSnake<T>;
type CamelObjectToSnake<T extends {[key: string]: any}> = {
[K in keyof T as CamelPropToSnake<K>]: T[K]
}
function camelCaseObject<T extends {[key: string]: any}>(obj: T) {
return Object.entries(obj)
.reduce((acc, [key, value]) =>
(acc[camelToSnakeCase(key as keyof T)] = value, acc),
{} as CamelObjectToSnake<T>
)
}
function camelToSnakeKeysOfArrayObject<T extends Array<{[key: string]: any}>>(arr: T) {
return arr.map(camelCaseObject) as {
[K in keyof T]: CamelObjectToSnake<T[K]>
};
}
camelToSnakeKeysOfArrayObject([{
aaAa: 1,
bbBb: true
}, {
aaAa: 'ggg'
}]);
'get' | 'set'
), методы (функциональный тип на инстансе) и static методы (функциональный тип на самом классе).declare class ViewHack {
getPosOnScale(currentPos: number): number;
}
describe('some method', () => {
test('should return smth', () => {
const view = new View('range-slider', settings);
jest.spyOn(view as unknown as ViewHack, 'getPosOnScale').mockReturnValue(100);
});
});
Мне лично на ум приходит такие как VBA
const zip = (...arrs) =>
arrs[0]?.map((n, i) => arrs.map(m => m[i])) ?? [];
const result = zip(arr1, arr2);
const zip = (arrs, defaultValue = null) =>
Array.from(
{ length: Math.max(...arrs.map(n => n.length)) },
(n, i) => arrs.map(m => i < m.length ? m[i] : defaultValue)
);
// или
const zip = (arrs, defaultValue = null) =>
arrs.reduce((acc, n, i) => (
n.forEach((m, j) => (acc[j] ??= Array(arrs.length).fill(defaultValue))[i] = m),
acc
), []);
const result = zip([ arr1, arr2 ]);
.zip([ [ 1, 2, 3 ], [], [ 99 ] ], Infinity)
- в результате получим[
[ 1, Infinity, 99 ],
[ 2, Infinity, Infinity ],
[ 3, Infinity, Infinity ]
]
function* zip(data, defaultValue = null) {
const iterators = Array.from(data, n => n[Symbol.iterator]());
for (let doneAll = false; (doneAll = !doneAll);) {
const values = [];
for (const n of iterators) {
const { value, done } = n.next();
values.push(done ? defaultValue : value);
doneAll &&= done;
}
if (!doneAll) {
yield values;
}
}
}
const result = [...zip([ arr1, arr2 ])];
.Array.from(zip((function*() {
yield [ , true, false, NaN ];
yield 'abcde';
yield Array(3).keys();
})()))
[
[ undefined, 'a', 0 ],
[ true, 'b', 1 ],
[ false, 'c', 2 ],
[ NaN, 'd', null ],
[ null, 'e', null ]
]
function call<R>(f: () => R): R {
return f()
}
class Result<T>{
public readonly result;
constructor(result: T){
this.result = result;
}
}
class NumberResult extends Result<number>{
constructor(result: number){
super(result);
}
public ResultPlusOne(){
return this.result + 1;
}
}
Result<T>
- это открытый дженерик, то есть в будущем на месте T должен быть указан другой\типы или типы для того чтобы создавать экземпляры класса.let result = new Result<number>(42)
class Result{
public readonly result;
constructor(result: number){
this.result = result;
}
}
class NumberResult extends Result<number>
class Base<T> {
protected val: T;
}
class A extends Base<string> {
methodA(): string {
return this.val; // Ok, так как здесь val имеет тип string
}
}
class B extends Base<number> {
methodB(): number {
return this.val; // тоже ok, так как здесь val имеет тип number
}
}
fs.writeFile(file, data[, options], callback)#
History:
- file
<string> | <Buffer> | <URL> | <integer>
filename or file descriptor- data
<string> | <Buffer> | <TypedArray> | <DataView> | <Object>
- options
<Object> | <string>
- encoding
<string> | <null>
Default: 'utf8'- mode
<integer>
Default: 0o666- flag
<string>
See support of file system flags. Default: 'w'.- signal
<AbortSignal>
allows aborting an in-progress writeFile- callback
<Function>
- err
<Error> | <AggregateError>
When file is a filename, asynchronously writes data to the file, replacing the file if it already exists. data can be a string or a buffer.
When file is a file descriptor, the behavior is similar to calling fs.write() directly (which is recommended). See the notes below on using a file descriptor.
The encoding option is ignored if data is a buffer.
If data is a plain object, it must have an own (not inherited) toString function property.
...
fs.write
). После этого нужные пункты оглавления подсветятся желтым и вы легко найдете нужный. document.querySelector('.case-print')