Home / File/ utils.ts — drizzle-orm Source File

utils.ts — drizzle-orm Source File

Architecture documentation for utils.ts, a typescript file in the drizzle-orm codebase. 0 imports, 2 dependents.

File typescript DrizzleSeed SeedOrchestration 2 dependents 7 functions

Entity Profile

Dependency Diagram

graph LR
  3590021a_2509_fc1e_c0e6_fb6ddfe693d4["utils.ts"]
  e5cee001_0354_7e76_ef0a_06dca71dc8ce["Generators.ts"]
  e5cee001_0354_7e76_ef0a_06dca71dc8ce --> 3590021a_2509_fc1e_c0e6_fb6ddfe693d4
  cddca4ed_9295_2c5a_7eae_0e65031c57b0["SeedService.ts"]
  cddca4ed_9295_2c5a_7eae_0e65031c57b0 --> 3590021a_2509_fc1e_c0e6_fb6ddfe693d4
  style 3590021a_2509_fc1e_c0e6_fb6ddfe693d4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

export const fastCartesianProduct = (sets: (number | string | boolean | object)[][], index: number) => {
	const resultList = [];
	let currSet: (typeof sets)[number];
	let element: (typeof sets)[number][number];

	for (let i = sets.length - 1; i >= 0; i--) {
		currSet = sets[i]!;
		element = currSet[index % currSet.length]!;
		resultList.unshift(element);
		index = Math.floor(index / currSet.length);
	}

	return resultList;
};

const sumArray = (weights: number[]) => {
	const scale = 1e10;
	const scaledSum = weights.reduce((acc, currVal) => acc + Math.round(currVal * scale), 0);
	return scaledSum / scale;
};

/**
 * @param weights positive number in range [0, 1], that represents probabilities to choose index of array. Example: weights = [0.2, 0.8]
 * @param [accuracy=100] approximate number of elements in returning array
 * @returns Example: with weights = [0.2, 0.8] and accuracy = 10 returning array of indices gonna equal this: [0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
 */
export const getWeightedIndices = (weights: number[], accuracy = 100) => {
	const weightsSum = sumArray(weights);
	if (weightsSum !== 1) {
		throw new Error(
			`The weights for the Weighted Random feature must add up to exactly 1. Please review your weights to ensure they total 1 before proceeding`,
		);
	}

	// const accuracy = 100;
	const weightedIndices: number[] = [];
	for (const [index, weight] of weights.entries()) {
		const ticketsNumb = Math.floor(weight * accuracy);
		weightedIndices.push(...Array.from<number>({ length: ticketsNumb }).fill(index));
	}

	return weightedIndices;
};

export const generateHashFromString = (s: string) => {
	let hash = 0;
	// p and m are prime numbers
	const p = 53;
	const m = 28871271685163;

	for (let i = 0; i < s.length; i++) {
		hash += ((s.codePointAt(i) || 0) * Math.pow(p, i)) % m;
	}

	return hash;
};

/**
 * @param param0.template example: "#####" or "#####-####"
 * @param param0.values example: ["3", "2", "h"]
 * @param param0.defaultValue example: "0"
 * @returns
 */
export const fillTemplate = ({ template, placeholdersCount, values, defaultValue = ' ' }: {
	template: string;
	placeholdersCount?: number;
	values: string[];
	defaultValue?: string;
}) => {
	if (placeholdersCount === undefined) {
		const iterArray = [...template.matchAll(/#/g)];
		placeholdersCount = iterArray.length;
	}

	const diff = placeholdersCount - values.length;
	if (diff > 0) {
		values.unshift(...Array.from<string>({ length: diff }).fill(defaultValue));
	}

	let resultStr = '', valueIdx = 0;
	for (const si of template) {
		if (si === '#') {
			resultStr += values[valueIdx];
			valueIdx += 1;
			continue;
		}
		resultStr += si;
	}

	return resultStr;
};

// is variable is object-like.
// Example:
// isObject({f: 4}) === true;
// isObject([1,2,3]) === false;
// isObject(new Set()) === false;
export const isObject = (value: any) => {
	if (value !== null && value !== undefined && value.constructor === Object) return true;
	return false;
};

export const equalSets = (set1: Set<any>, set2: Set<any>) => {
	return set1.size === set2.size && [...set1].every((si) => set2.has(si));
};

Domain

Subdomains

Frequently Asked Questions

What does utils.ts do?
utils.ts is a source file in the drizzle-orm codebase, written in typescript. It belongs to the DrizzleSeed domain, SeedOrchestration subdomain.
What functions are defined in utils.ts?
utils.ts defines 7 function(s): equalSets, fastCartesianProduct, fillTemplate, generateHashFromString, getWeightedIndices, isObject, sumArray.
What files import utils.ts?
utils.ts is imported by 2 file(s): Generators.ts, SeedService.ts.
Where is utils.ts in the architecture?
utils.ts is located at drizzle-seed/src/services/utils.ts (domain: DrizzleSeed, subdomain: SeedOrchestration, directory: drizzle-seed/src/services).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free