mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 10:54:44 +00:00
feat(packages): add version jsdoc tags
This commit is contained in:
@@ -1,7 +1,11 @@
|
|||||||
|
// TODO: tests
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @name _global
|
* @name _global
|
||||||
* @category Multi
|
* @category Multi
|
||||||
* @description Global object that works in any environment
|
* @description Global object that works in any environment
|
||||||
|
*
|
||||||
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
export const _global =
|
export const _global =
|
||||||
typeof globalThis !== 'undefined'
|
typeof globalThis !== 'undefined'
|
||||||
@@ -12,4 +16,4 @@ export const _global =
|
|||||||
? global
|
? global
|
||||||
: typeof self !== 'undefined'
|
: typeof self !== 'undefined'
|
||||||
? self
|
? self
|
||||||
: {};
|
: undefined;
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
export * from './global';
|
export * from './global';
|
||||||
export * from './debounce';
|
// export * from './debounce';
|
||||||
@@ -3,6 +3,8 @@
|
|||||||
*
|
*
|
||||||
* @returns {Function} A function that generates unique flags
|
* @returns {Function} A function that generates unique flags
|
||||||
* @throws {RangeError} If more than 31 flags are created
|
* @throws {RangeError} If more than 31 flags are created
|
||||||
|
*
|
||||||
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
export function flagsGenerator() {
|
export function flagsGenerator() {
|
||||||
let lastFlag = 0;
|
let lastFlag = 0;
|
||||||
@@ -22,6 +24,8 @@ export function flagsGenerator() {
|
|||||||
*
|
*
|
||||||
* @param {number[]} flags - The flags to combine
|
* @param {number[]} flags - The flags to combine
|
||||||
* @returns {number} The combined flags
|
* @returns {number} The combined flags
|
||||||
|
*
|
||||||
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
export function and(...flags: number[]) {
|
export function and(...flags: number[]) {
|
||||||
return flags.reduce((acc, flag) => acc & flag, -1);
|
return flags.reduce((acc, flag) => acc & flag, -1);
|
||||||
@@ -32,6 +36,8 @@ export function and(...flags: number[]) {
|
|||||||
*
|
*
|
||||||
* @param {number[]} flags - The flags to combine
|
* @param {number[]} flags - The flags to combine
|
||||||
* @returns {number} The combined flags
|
* @returns {number} The combined flags
|
||||||
|
*
|
||||||
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
export function or(...flags: number[]) {
|
export function or(...flags: number[]) {
|
||||||
return flags.reduce((acc, flag) => acc | flag, 0);
|
return flags.reduce((acc, flag) => acc | flag, 0);
|
||||||
@@ -42,6 +48,8 @@ export function or(...flags: number[]) {
|
|||||||
*
|
*
|
||||||
* @param {number} flag - The flag to apply the NOT operator to
|
* @param {number} flag - The flag to apply the NOT operator to
|
||||||
* @returns {number} The result of the NOT operator
|
* @returns {number} The result of the NOT operator
|
||||||
|
*
|
||||||
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
export function not(flag: number) {
|
export function not(flag: number) {
|
||||||
return ~flag;
|
return ~flag;
|
||||||
@@ -51,7 +59,10 @@ export function not(flag: number) {
|
|||||||
* Function to make sure a flag has a specific bit set
|
* Function to make sure a flag has a specific bit set
|
||||||
*
|
*
|
||||||
* @param {number} flag - The flag to check
|
* @param {number} flag - The flag to check
|
||||||
|
* @param {number} other - Flag to check
|
||||||
* @returns {boolean} Whether the flag has the bit set
|
* @returns {boolean} Whether the flag has the bit set
|
||||||
|
*
|
||||||
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
export function has(flag: number, other: number) {
|
export function has(flag: number, other: number) {
|
||||||
return (flag & other) === other;
|
return (flag & other) === other;
|
||||||
@@ -62,6 +73,8 @@ export function has(flag: number, other: number) {
|
|||||||
*
|
*
|
||||||
* @param {number} flag - The flag to check
|
* @param {number} flag - The flag to check
|
||||||
* @returns {boolean} Whether the flag is set
|
* @returns {boolean} Whether the flag is set
|
||||||
|
*
|
||||||
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
export function is(flag: number) {
|
export function is(flag: number) {
|
||||||
return flag !== 0;
|
return flag !== 0;
|
||||||
@@ -73,6 +86,8 @@ export function is(flag: number) {
|
|||||||
* @param {number} flag - Source flag
|
* @param {number} flag - Source flag
|
||||||
* @param {number} other - Flag to unset
|
* @param {number} other - Flag to unset
|
||||||
* @returns {number} The new flag
|
* @returns {number} The new flag
|
||||||
|
*
|
||||||
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
export function unset(flag: number, other: number) {
|
export function unset(flag: number, other: number) {
|
||||||
return flag & ~other;
|
return flag & ~other;
|
||||||
@@ -84,6 +99,8 @@ export function unset(flag: number, other: number) {
|
|||||||
* @param {number} flag - Source flag
|
* @param {number} flag - Source flag
|
||||||
* @param {number} other - Flag to toggle
|
* @param {number} other - Flag to toggle
|
||||||
* @returns {number} The new flag
|
* @returns {number} The new flag
|
||||||
|
*
|
||||||
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
export function toggle(flag: number, other: number) {
|
export function toggle(flag: number, other: number) {
|
||||||
return flag ^ other;
|
return flag ^ other;
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ export type EventsRecord = Record<string, Subscriber>;
|
|||||||
/**
|
/**
|
||||||
* Simple PubSub implementation
|
* Simple PubSub implementation
|
||||||
*
|
*
|
||||||
|
* @since 0.0.2
|
||||||
|
*
|
||||||
* @template {EventsRecord} Events
|
* @template {EventsRecord} Events
|
||||||
*/
|
*/
|
||||||
export class PubSub<Events extends EventsRecord> {
|
export class PubSub<Events extends EventsRecord> {
|
||||||
@@ -85,7 +87,7 @@ export class PubSub<Events extends EventsRecord> {
|
|||||||
* @param {...Parameters<Events[K]>} args Arguments for the listener
|
* @param {...Parameters<Events[K]>} args Arguments for the listener
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
public emit<K extends keyof Events>(event: K, ...args: Parameters<Events[K]>): boolean {
|
public emit<K extends keyof Events>(event: K, ...args: Parameters<Events[K]>) {
|
||||||
const listeners = this.events.get(event);
|
const listeners = this.events.get(event);
|
||||||
|
|
||||||
if (!listeners)
|
if (!listeners)
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ export type StackOptions = {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a stack data structure
|
* Represents a stack data structure
|
||||||
|
*
|
||||||
|
* @since 0.0.2
|
||||||
|
*
|
||||||
* @template T The type of elements stored in the stack
|
* @template T The type of elements stored in the stack
|
||||||
*/
|
*/
|
||||||
export class Stack<T> implements Iterable<T>, AsyncIterable<T> {
|
export class Stack<T> implements Iterable<T>, AsyncIterable<T> {
|
||||||
@@ -13,7 +16,7 @@ export class Stack<T> implements Iterable<T>, AsyncIterable<T> {
|
|||||||
* @private
|
* @private
|
||||||
* @type {number}
|
* @type {number}
|
||||||
*/
|
*/
|
||||||
private maxSize: number;
|
private readonly maxSize: number;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The stack data structure
|
* The stack data structure
|
||||||
@@ -21,7 +24,7 @@ export class Stack<T> implements Iterable<T>, AsyncIterable<T> {
|
|||||||
* @private
|
* @private
|
||||||
* @type {T[]}
|
* @type {T[]}
|
||||||
*/
|
*/
|
||||||
private stack: T[];
|
private readonly stack: T[];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an instance of Stack
|
* Creates an instance of Stack
|
||||||
@@ -39,7 +42,7 @@ export class Stack<T> implements Iterable<T>, AsyncIterable<T> {
|
|||||||
* Gets the number of elements in the stack
|
* Gets the number of elements in the stack
|
||||||
* @returns {number} The number of elements in the stack
|
* @returns {number} The number of elements in the stack
|
||||||
*/
|
*/
|
||||||
public get length(): number {
|
public get length() {
|
||||||
return this.stack.length;
|
return this.stack.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,7 +50,7 @@ export class Stack<T> implements Iterable<T>, AsyncIterable<T> {
|
|||||||
* Checks if the stack is empty
|
* Checks if the stack is empty
|
||||||
* @returns {boolean} `true` if the stack is empty, `false` otherwise
|
* @returns {boolean} `true` if the stack is empty, `false` otherwise
|
||||||
*/
|
*/
|
||||||
public get isEmpty(): boolean {
|
public get isEmpty() {
|
||||||
return this.stack.length === 0;
|
return this.stack.length === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,7 +58,7 @@ export class Stack<T> implements Iterable<T>, AsyncIterable<T> {
|
|||||||
* Checks if the stack is full
|
* Checks if the stack is full
|
||||||
* @returns {boolean} `true` if the stack is full, `false` otherwise
|
* @returns {boolean} `true` if the stack is full, `false` otherwise
|
||||||
*/
|
*/
|
||||||
public get isFull(): boolean {
|
public get isFull() {
|
||||||
return this.stack.length === this.maxSize;
|
return this.stack.length === this.maxSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +81,7 @@ export class Stack<T> implements Iterable<T>, AsyncIterable<T> {
|
|||||||
* Pops an element from the stack
|
* Pops an element from the stack
|
||||||
* @returns {T} The element popped from the stack
|
* @returns {T} The element popped from the stack
|
||||||
*/
|
*/
|
||||||
public pop(): T | undefined {
|
public pop() {
|
||||||
return this.stack.pop();
|
return this.stack.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,7 +89,7 @@ export class Stack<T> implements Iterable<T>, AsyncIterable<T> {
|
|||||||
* Peeks at the top element of the stack
|
* Peeks at the top element of the stack
|
||||||
* @returns {T} The top element of the stack
|
* @returns {T} The top element of the stack
|
||||||
*/
|
*/
|
||||||
public peek(): T | undefined {
|
public peek() {
|
||||||
if (this.isEmpty)
|
if (this.isEmpty)
|
||||||
throw new RangeError('Stack is empty');
|
throw new RangeError('Stack is empty');
|
||||||
|
|
||||||
@@ -109,7 +112,7 @@ export class Stack<T> implements Iterable<T>, AsyncIterable<T> {
|
|||||||
*
|
*
|
||||||
* @returns {T[]}
|
* @returns {T[]}
|
||||||
*/
|
*/
|
||||||
public toArray(): T[] {
|
public toArray() {
|
||||||
return this.stack.toReversed();
|
return this.stack.toReversed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,12 +4,12 @@
|
|||||||
* @param {string} left First string
|
* @param {string} left First string
|
||||||
* @param {string} right Second string
|
* @param {string} right Second string
|
||||||
* @returns {number} The Levenshtein distance between the two strings
|
* @returns {number} The Levenshtein distance between the two strings
|
||||||
|
*
|
||||||
|
* @since 0.0.1
|
||||||
*/
|
*/
|
||||||
export function levenshteinDistance(left: string, right: string): number {
|
export function levenshteinDistance(left: string, right: string): number {
|
||||||
// If the strings are equal, the distance is 0
|
|
||||||
if (left === right) return 0;
|
if (left === right) return 0;
|
||||||
|
|
||||||
// If either string is empty, the distance is the length of the other string
|
|
||||||
if (left.length === 0) return right.length;
|
if (left.length === 0) return right.length;
|
||||||
if (right.length === 0) return left.length;
|
if (right.length === 0) return left.length;
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ export type Trigrams = Map<string, number>;
|
|||||||
*
|
*
|
||||||
* @param {string} text The text to extract trigrams
|
* @param {string} text The text to extract trigrams
|
||||||
* @returns {Trigrams} A map of trigram to count
|
* @returns {Trigrams} A map of trigram to count
|
||||||
|
*
|
||||||
|
* @since 0.0.1
|
||||||
*/
|
*/
|
||||||
export function trigramProfile(text: string): Trigrams {
|
export function trigramProfile(text: string): Trigrams {
|
||||||
text = '\n\n' + text + '\n\n';
|
text = '\n\n' + text + '\n\n';
|
||||||
@@ -26,6 +28,8 @@ export function trigramProfile(text: string): Trigrams {
|
|||||||
* @param {Trigrams} left First text trigram profile
|
* @param {Trigrams} left First text trigram profile
|
||||||
* @param {Trigrams} right Second text trigram profile
|
* @param {Trigrams} right Second text trigram profile
|
||||||
* @returns {number} The trigram distance between the two strings
|
* @returns {number} The trigram distance between the two strings
|
||||||
|
*
|
||||||
|
* @since 0.0.1
|
||||||
*/
|
*/
|
||||||
export function trigramDistance(left: Trigrams, right: Trigrams): number {
|
export function trigramDistance(left: Trigrams, right: Trigrams): number {
|
||||||
let distance = -4;
|
let distance = -4;
|
||||||
|
|||||||
Reference in New Issue
Block a user