mirror of
https://github.com/swc-project/swc.git
synced 2024-12-23 21:54:36 +03:00
38 lines
764 B
TypeScript
38 lines
764 B
TypeScript
|
// 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);
|
||
|
};
|