@web5/common
Advanced tools
Comparing version 0.2.2 to 0.2.3-alpha-20240202-c00b50c
@@ -17,2 +17,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
import { base32z } from 'multiformats/bases/base32'; | ||
import { base58btc } from 'multiformats/bases/base58'; | ||
@@ -37,2 +38,5 @@ import { base64url } from 'multiformats/bases/base64'; | ||
} | ||
static base32Z(data) { | ||
return new Convert(data, 'Base32Z'); | ||
} | ||
static base58Btc(data) { | ||
@@ -128,2 +132,11 @@ return new Convert(data, 'Base58Btc'); | ||
} | ||
toBase32Z() { | ||
switch (this.format) { | ||
case 'Uint8Array': { | ||
return base32z.baseEncode(this.data); | ||
} | ||
default: | ||
throw new TypeError(`Conversion from ${this.format} to Base64Z is not supported.`); | ||
} | ||
} | ||
toBase58Btc() { | ||
@@ -340,2 +353,5 @@ switch (this.format) { | ||
} | ||
case 'Base32Z': { | ||
return base32z.baseDecode(this.data); | ||
} | ||
case 'Base58Btc': { | ||
@@ -342,0 +358,0 @@ return base58btc.baseDecode(this.data); |
@@ -12,4 +12,4 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
export class LevelStore { | ||
constructor(location = 'DATASTORE') { | ||
this.store = new Level(location); | ||
constructor({ db, location = 'DATASTORE' } = {}) { | ||
this.store = db !== null && db !== void 0 ? db : new Level(location); | ||
} | ||
@@ -29,3 +29,2 @@ clear() { | ||
yield this.store.del(key); | ||
return true; | ||
}); | ||
@@ -35,3 +34,11 @@ } | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.store.get(key); | ||
try { | ||
return yield this.store.get(key); | ||
} | ||
catch (error) { | ||
// Don't throw when a key wasn't found. | ||
if (error.notFound) | ||
return undefined; | ||
throw error; | ||
} | ||
}); | ||
@@ -38,0 +45,0 @@ } |
@@ -8,2 +8,3 @@ import type { Multibase } from 'multiformats'; | ||
static asyncIterable(data: AsyncIterable<any>): Convert; | ||
static base32Z(data: string): Convert; | ||
static base58Btc(data: string): Convert; | ||
@@ -25,2 +26,3 @@ static base64Url(data: string): Convert; | ||
toArrayBufferAsync(): Promise<ArrayBuffer>; | ||
toBase32Z(): string; | ||
toBase58Btc(): string; | ||
@@ -27,0 +29,0 @@ toBase64Url(): string; |
@@ -0,10 +1,15 @@ | ||
/// <reference types="node" resolution-mode="require"/> | ||
import type { AbstractLevel } from 'abstract-level'; | ||
import type { KeyValueStore } from './types.js'; | ||
export declare class LevelStore implements KeyValueStore<string, any> { | ||
export declare class LevelStore<K = string, V = any> implements KeyValueStore<K, V> { | ||
private store; | ||
constructor(location?: string); | ||
constructor({ db, location }?: { | ||
db?: AbstractLevel<string | Buffer | Uint8Array, K, V>; | ||
location?: string; | ||
}); | ||
clear(): Promise<void>; | ||
close(): Promise<void>; | ||
delete(key: string): Promise<boolean>; | ||
get(key: string): Promise<any>; | ||
set(key: string, value: any): Promise<void>; | ||
delete(key: K): Promise<void>; | ||
get(key: K): Promise<V | undefined>; | ||
set(key: K, value: V): Promise<void>; | ||
} | ||
@@ -11,0 +16,0 @@ /** |
/** | ||
* Represents an array of a fixed length, preventing modifications to its size. | ||
* | ||
* The `FixedLengthArray` utility type transforms a standard array into a variant where | ||
* methods that could alter the length are omitted. It leverages TypeScript's advanced types, | ||
* such as conditional types and mapped types, to ensure that the array cannot be resized | ||
* through methods like `push`, `pop`, `splice`, `shift`, and `unshift`. The utility type | ||
* maintains all other characteristics of a standard array, including indexing, iteration, | ||
* and type checking for its elements. | ||
* | ||
* Note: The type does not prevent direct assignment to indices, even if it would exceed | ||
* the original length. However, such actions would lead to TypeScript type errors. | ||
* | ||
* @example | ||
* ```ts | ||
* // Declare a variable with a type of fixed-length array of three strings. | ||
* let myFixedLengthArray: FixedLengthArray< [string, string, string]>; | ||
* | ||
* // Array declaration tests | ||
* myFixedLengthArray = [ 'a', 'b', 'c' ]; // OK | ||
* myFixedLengthArray = [ 'a', 'b', 123 ]; // TYPE ERROR | ||
* myFixedLengthArray = [ 'a' ]; // LENGTH ERROR | ||
* myFixedLengthArray = [ 'a', 'b' ]; // LENGTH ERROR | ||
* | ||
* // Index assignment tests | ||
* myFixedLengthArray[1] = 'foo'; // OK | ||
* myFixedLengthArray[1000] = 'foo'; // INVALID INDEX ERROR | ||
* | ||
* // Methods that mutate array length | ||
* myFixedLengthArray.push('foo'); // MISSING METHOD ERROR | ||
* myFixedLengthArray.pop(); // MISSING METHOD ERROR | ||
* | ||
* // Direct length manipulation | ||
* myFixedLengthArray.length = 123; // READ-ONLY ERROR | ||
* | ||
* // Destructuring | ||
* let [ a ] = myFixedLengthArray; // OK | ||
* let [ a, b ] = myFixedLengthArray; // OK | ||
* let [ a, b, c ] = myFixedLengthArray; // OK | ||
* let [ a, b, c, d ] = myFixedLengthArray; // INVALID INDEX ERROR | ||
* ``` | ||
* | ||
* @template T extends any[] - The array type to be transformed. | ||
*/ | ||
export type FixedLengthArray<T extends any[]> = Pick<T, Exclude<keyof T, ArrayLengthMutationKeys>> & { | ||
/** | ||
* Custom iterator for the `FixedLengthArray` type. | ||
* | ||
* This iterator allows the `FixedLengthArray` to be used in standard iteration | ||
* contexts, such as `for...of` loops and spread syntax. It ensures that even though | ||
* the array is of a fixed length with disabled mutation methods, it still retains | ||
* iterable behavior similar to a regular array. | ||
* | ||
* @returns An IterableIterator for the array items. | ||
*/ | ||
[Symbol.iterator]: () => IterableIterator<ArrayItems<T>>; | ||
}; | ||
/** Helper types for {@link FixedLengthArray} */ | ||
type ArrayLengthMutationKeys = 'splice' | 'push' | 'pop' | 'shift' | 'unshift' | number; | ||
type ArrayItems<T extends Array<any>> = T extends Array<infer TItems> ? TItems : never; | ||
/** | ||
* isArrayBufferSlice | ||
@@ -63,2 +123,29 @@ * | ||
/** | ||
* Utility type that transforms a type `T` to have only certain keys `K` as required, while the | ||
* rest remain optional, except for keys specified in `O`, which are omitted entirely. | ||
* | ||
* This type is useful when you need a variation of a type where only specific properties are | ||
* required, and others are either optional or not included at all. It allows for more flexible type | ||
* definitions based on existing types without the need to redefine them. | ||
* | ||
* @template T - The original type to be transformed. | ||
* @template K - The keys of `T` that should be required. | ||
* @template O - The keys of `T` that should be omitted from the resulting type (optional). | ||
* | ||
* @example | ||
* ```ts | ||
* // Given an interface | ||
* interface Example { | ||
* requiredProp: string; | ||
* optionalProp?: number; | ||
* anotherOptionalProp?: boolean; | ||
* } | ||
* | ||
* // Making 'optionalProp' required and omitting 'anotherOptionalProp' | ||
* type ModifiedExample = RequireOnly<Example, 'optionalProp', 'anotherOptionalProp'>; | ||
* // Result: { requiredProp?: string; optionalProp: number; } | ||
* ``` | ||
*/ | ||
export type RequireOnly<T, K extends keyof T, O extends keyof T = never> = Required<Pick<T, K>> & Omit<Partial<T>, O>; | ||
/** | ||
* universalTypeOf | ||
@@ -97,2 +184,25 @@ * | ||
export declare function universalTypeOf(value: unknown): string; | ||
/** | ||
* Utility type to extract the type resolved by a Promise. | ||
* | ||
* This type unwraps the type `T` from `Promise<T>` if `T` is a Promise, otherwise returns `T` as | ||
* is. It's useful in situations where you need to handle the type returned by a promise-based | ||
* function in a synchronous context, such as defining types for test vectors or handling return | ||
* types in non-async code blocks. | ||
* | ||
* @template T - The type to unwrap from the Promise. | ||
* | ||
* @example | ||
* ```ts | ||
* // For a Promise type, it extracts the resolved type. | ||
* type AsyncNumber = Promise<number>; | ||
* type UnwrappedNumber = UnwrapPromise<AsyncNumber>; // number | ||
* | ||
* // For a non-Promise type, it returns the type as is. | ||
* type StringValue = string; | ||
* type UnwrappedString = UnwrapPromise<StringValue>; // string | ||
* ``` | ||
*/ | ||
export type UnwrapPromise<T> = T extends Promise<infer U> ? U : T; | ||
export {}; | ||
//# sourceMappingURL=type-utils.d.ts.map |
@@ -40,6 +40,2 @@ /** | ||
} | ||
/** | ||
* Represents an object type where a subset of keys are required and everything else is optional. | ||
*/ | ||
export type RequireOnly<T, K extends keyof T, O extends keyof T = never> = Required<Pick<T, K>> & Omit<Partial<T>, O>; | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "@web5/common", | ||
"version": "0.2.2", | ||
"version": "0.2.3-alpha-20240202-c00b50c", | ||
"type": "module", | ||
@@ -86,3 +86,3 @@ "main": "./dist/cjs/index.js", | ||
"@web/test-runner-playwright": "0.11.0", | ||
"c8": "8.0.1", | ||
"c8": "9.0.0", | ||
"chai": "4.3.10", | ||
@@ -94,2 +94,3 @@ "chai-as-promised": "7.1.1", | ||
"mocha": "10.2.0", | ||
"mocha-junit-reporter": "2.2.1", | ||
"playwright": "1.40.1", | ||
@@ -96,0 +97,0 @@ "rimraf": "4.4.0", |
import type { Multibase } from 'multiformats'; | ||
import { base32z } from 'multiformats/bases/base32'; | ||
import { base58btc } from 'multiformats/bases/base58'; | ||
@@ -31,2 +32,6 @@ import { base64url } from 'multiformats/bases/base64'; | ||
static base32Z(data: string): Convert { | ||
return new Convert(data, 'Base32Z'); | ||
} | ||
static base58Btc(data: string): Convert { | ||
@@ -135,2 +140,14 @@ return new Convert(data, 'Base58Btc'); | ||
toBase32Z(): string { | ||
switch (this.format) { | ||
case 'Uint8Array': { | ||
return base32z.baseEncode(this.data); | ||
} | ||
default: | ||
throw new TypeError(`Conversion from ${this.format} to Base64Z is not supported.`); | ||
} | ||
} | ||
toBase58Btc(): string { | ||
@@ -362,2 +379,6 @@ switch (this.format) { | ||
case 'Base32Z': { | ||
return base32z.baseDecode(this.data); | ||
} | ||
case 'Base58Btc': { | ||
@@ -364,0 +385,0 @@ return base58btc.baseDecode(this.data); |
@@ -0,1 +1,3 @@ | ||
import type { AbstractLevel } from 'abstract-level'; | ||
import { Level } from 'level'; | ||
@@ -5,7 +7,10 @@ | ||
export class LevelStore implements KeyValueStore<string, any> { | ||
private store: Level<string, string>; | ||
export class LevelStore<K = string, V = any> implements KeyValueStore<K, V> { | ||
private store: AbstractLevel<string | Buffer | Uint8Array, K, V>; | ||
constructor(location = 'DATASTORE') { | ||
this.store = new Level(location); | ||
constructor({ db, location = 'DATASTORE' }: { | ||
db?: AbstractLevel<string | Buffer | Uint8Array, K, V>; | ||
location?: string; | ||
} = {}) { | ||
this.store = db ?? new Level<K, V>(location); | ||
} | ||
@@ -21,12 +26,17 @@ | ||
async delete(key: string): Promise<boolean> { | ||
async delete(key: K): Promise<void> { | ||
await this.store.del(key); | ||
return true; | ||
} | ||
async get(key: string): Promise<any> { | ||
return await this.store.get(key); | ||
async get(key: K): Promise<V | undefined> { | ||
try { | ||
return await this.store.get(key); | ||
} catch (error: any) { | ||
// Don't throw when a key wasn't found. | ||
if (error.notFound) return undefined; | ||
throw error; | ||
} | ||
} | ||
async set(key: string, value: any): Promise<void> { | ||
async set(key: K, value: V): Promise<void> { | ||
await this.store.put(key, value); | ||
@@ -33,0 +43,0 @@ } |
/** | ||
* Represents an array of a fixed length, preventing modifications to its size. | ||
* | ||
* The `FixedLengthArray` utility type transforms a standard array into a variant where | ||
* methods that could alter the length are omitted. It leverages TypeScript's advanced types, | ||
* such as conditional types and mapped types, to ensure that the array cannot be resized | ||
* through methods like `push`, `pop`, `splice`, `shift`, and `unshift`. The utility type | ||
* maintains all other characteristics of a standard array, including indexing, iteration, | ||
* and type checking for its elements. | ||
* | ||
* Note: The type does not prevent direct assignment to indices, even if it would exceed | ||
* the original length. However, such actions would lead to TypeScript type errors. | ||
* | ||
* @example | ||
* ```ts | ||
* // Declare a variable with a type of fixed-length array of three strings. | ||
* let myFixedLengthArray: FixedLengthArray< [string, string, string]>; | ||
* | ||
* // Array declaration tests | ||
* myFixedLengthArray = [ 'a', 'b', 'c' ]; // OK | ||
* myFixedLengthArray = [ 'a', 'b', 123 ]; // TYPE ERROR | ||
* myFixedLengthArray = [ 'a' ]; // LENGTH ERROR | ||
* myFixedLengthArray = [ 'a', 'b' ]; // LENGTH ERROR | ||
* | ||
* // Index assignment tests | ||
* myFixedLengthArray[1] = 'foo'; // OK | ||
* myFixedLengthArray[1000] = 'foo'; // INVALID INDEX ERROR | ||
* | ||
* // Methods that mutate array length | ||
* myFixedLengthArray.push('foo'); // MISSING METHOD ERROR | ||
* myFixedLengthArray.pop(); // MISSING METHOD ERROR | ||
* | ||
* // Direct length manipulation | ||
* myFixedLengthArray.length = 123; // READ-ONLY ERROR | ||
* | ||
* // Destructuring | ||
* let [ a ] = myFixedLengthArray; // OK | ||
* let [ a, b ] = myFixedLengthArray; // OK | ||
* let [ a, b, c ] = myFixedLengthArray; // OK | ||
* let [ a, b, c, d ] = myFixedLengthArray; // INVALID INDEX ERROR | ||
* ``` | ||
* | ||
* @template T extends any[] - The array type to be transformed. | ||
*/ | ||
export type FixedLengthArray<T extends any[]> = | ||
Pick<T, Exclude<keyof T, ArrayLengthMutationKeys>> | ||
& { | ||
/** | ||
* Custom iterator for the `FixedLengthArray` type. | ||
* | ||
* This iterator allows the `FixedLengthArray` to be used in standard iteration | ||
* contexts, such as `for...of` loops and spread syntax. It ensures that even though | ||
* the array is of a fixed length with disabled mutation methods, it still retains | ||
* iterable behavior similar to a regular array. | ||
* | ||
* @returns An IterableIterator for the array items. | ||
*/ | ||
[Symbol.iterator]: () => IterableIterator<ArrayItems<T>> | ||
}; | ||
/** Helper types for {@link FixedLengthArray} */ | ||
type ArrayLengthMutationKeys = 'splice' | 'push' | 'pop' | 'shift' | 'unshift' | number; | ||
type ArrayItems<T extends Array<any>> = T extends Array<infer TItems> ? TItems : never; | ||
/** | ||
* isArrayBufferSlice | ||
@@ -76,2 +140,30 @@ * | ||
/** | ||
* Utility type that transforms a type `T` to have only certain keys `K` as required, while the | ||
* rest remain optional, except for keys specified in `O`, which are omitted entirely. | ||
* | ||
* This type is useful when you need a variation of a type where only specific properties are | ||
* required, and others are either optional or not included at all. It allows for more flexible type | ||
* definitions based on existing types without the need to redefine them. | ||
* | ||
* @template T - The original type to be transformed. | ||
* @template K - The keys of `T` that should be required. | ||
* @template O - The keys of `T` that should be omitted from the resulting type (optional). | ||
* | ||
* @example | ||
* ```ts | ||
* // Given an interface | ||
* interface Example { | ||
* requiredProp: string; | ||
* optionalProp?: number; | ||
* anotherOptionalProp?: boolean; | ||
* } | ||
* | ||
* // Making 'optionalProp' required and omitting 'anotherOptionalProp' | ||
* type ModifiedExample = RequireOnly<Example, 'optionalProp', 'anotherOptionalProp'>; | ||
* // Result: { requiredProp?: string; optionalProp: number; } | ||
* ``` | ||
*/ | ||
export type RequireOnly<T, K extends keyof T, O extends keyof T = never> = Required<Pick<T, K>> & Omit<Partial<T>, O>; | ||
/** | ||
* universalTypeOf | ||
@@ -118,2 +210,25 @@ * | ||
return type; | ||
} | ||
} | ||
/** | ||
* Utility type to extract the type resolved by a Promise. | ||
* | ||
* This type unwraps the type `T` from `Promise<T>` if `T` is a Promise, otherwise returns `T` as | ||
* is. It's useful in situations where you need to handle the type returned by a promise-based | ||
* function in a synchronous context, such as defining types for test vectors or handling return | ||
* types in non-async code blocks. | ||
* | ||
* @template T - The type to unwrap from the Promise. | ||
* | ||
* @example | ||
* ```ts | ||
* // For a Promise type, it extracts the resolved type. | ||
* type AsyncNumber = Promise<number>; | ||
* type UnwrappedNumber = UnwrapPromise<AsyncNumber>; // number | ||
* | ||
* // For a non-Promise type, it returns the type as is. | ||
* type StringValue = string; | ||
* type UnwrappedString = UnwrapPromise<StringValue>; // string | ||
* ``` | ||
*/ | ||
export type UnwrapPromise<T> = T extends Promise<infer U> ? U : T; |
@@ -43,7 +43,2 @@ /** | ||
set(key: K, value: V): Promise<void>; | ||
} | ||
/** | ||
* Represents an object type where a subset of keys are required and everything else is optional. | ||
*/ | ||
export type RequireOnly<T, K extends keyof T, O extends keyof T = never> = Required<Pick<T, K>> & Omit<Partial<T>, O>; | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2363042
8111
21