swc/bundler/tests/.cache/deno/5707df2773cbf05df35503cd5a8cc873455ad561.ts

38 lines
764 B
TypeScript
Raw Normal View History

// Loaded from https://deno.land/x/segno@v1.1.0/lib/validations/isByteLength.ts
// @ts-ignore allowing typedoc to build
import { assertString } from '../helpers/assertString.ts';
type ByteLengthOptions = {
/**
* @default 0
*/
min?: number;
/**
* @default undefined
*/
max?: number;
};
/**
* @ignore
*/
const defaultByteLengthOptions: ByteLengthOptions = {
min: 0,
};
export const isByteLength = (str: string, options?: ByteLengthOptions) => {
assertString(str);
options = { ...defaultByteLengthOptions, ...options };
let { min, max } = options;
// force min to be 0 if undefined
min = min || 0;
const len = encodeURI(str).split(/%..|./).length - 1;
return len >= min && (typeof max === 'undefined' || len <= max);
};