Skip to content

Commit

Permalink
Refactor byte count in byte actions
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Oct 11, 2024
1 parent 2cf6025 commit 1ecad75
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 8 deletions.
1 change: 1 addition & 0 deletions library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to the library will be documented in this file.
- Add `checkItemsAsync` action (pull request #856)
- Add `graphemes`, `maxGraphemes`, `minGraphemes` and `notGraphemes` action (pull request #853)
- Change types and implementation to support Standard Schema
- Refactor `bytes`, `maxBytes`, `minBytes` and `notBytes` action

## v0.42.1 (September 20, 2024)

Expand Down
4 changes: 2 additions & 2 deletions library/src/actions/bytes/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
BaseValidation,
ErrorMessage,
} from '../../types/index.ts';
import { _addIssue } from '../../utils/index.ts';
import { _addIssue, _getByteCount } from '../../utils/index.ts';

/**
* Bytes issue type.
Expand Down Expand Up @@ -110,7 +110,7 @@ export function bytes(
message,
'~validate'(dataset, config) {
if (dataset.typed) {
const length = new TextEncoder().encode(dataset.value).length;
const length = _getByteCount(dataset.value);
if (length !== this.requirement) {
_addIssue(this, 'bytes', dataset, config, {
received: `${length}`,
Expand Down
4 changes: 2 additions & 2 deletions library/src/actions/maxBytes/maxBytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
BaseValidation,
ErrorMessage,
} from '../../types/index.ts';
import { _addIssue } from '../../utils/index.ts';
import { _addIssue, _getByteCount } from '../../utils/index.ts';

/**
* Max bytes issue type.
Expand Down Expand Up @@ -115,7 +115,7 @@ export function maxBytes(
message,
'~validate'(dataset, config) {
if (dataset.typed) {
const length = new TextEncoder().encode(dataset.value).length;
const length = _getByteCount(dataset.value);
if (length > this.requirement) {
_addIssue(this, 'bytes', dataset, config, {
received: `${length}`,
Expand Down
4 changes: 2 additions & 2 deletions library/src/actions/minBytes/minBytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
BaseValidation,
ErrorMessage,
} from '../../types/index.ts';
import { _addIssue } from '../../utils/index.ts';
import { _addIssue, _getByteCount } from '../../utils/index.ts';

/**
* Min bytes issue type.
Expand Down Expand Up @@ -115,7 +115,7 @@ export function minBytes(
message,
'~validate'(dataset, config) {
if (dataset.typed) {
const length = new TextEncoder().encode(dataset.value).length;
const length = _getByteCount(dataset.value);
if (length < this.requirement) {
_addIssue(this, 'bytes', dataset, config, {
received: `${length}`,
Expand Down
4 changes: 2 additions & 2 deletions library/src/actions/notBytes/notBytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
BaseValidation,
ErrorMessage,
} from '../../types/index.ts';
import { _addIssue } from '../../utils/index.ts';
import { _addIssue, _getByteCount } from '../../utils/index.ts';

/**
* Not bytes issue type.
Expand Down Expand Up @@ -115,7 +115,7 @@ export function notBytes(
message,
'~validate'(dataset, config) {
if (dataset.typed) {
const length = new TextEncoder().encode(dataset.value).length;
const length = _getByteCount(dataset.value);
if (length === this.requirement) {
_addIssue(this, 'bytes', dataset, config, {
received: `${length}`,
Expand Down
12 changes: 12 additions & 0 deletions library/src/utils/_getByteCount/_getByteCount.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, expect, test } from 'vitest';
import { _getByteCount } from './_getByteCount.ts';

describe('_getByteCount', () => {
test('should return byte count', () => {
expect(_getByteCount('hello world')).toBe(11);
expect(_getByteCount('😀')).toBe(4);
expect(_getByteCount('🧑🏻‍💻')).toBe(15);
expect(_getByteCount('𝄞')).toBe(4);
expect(_getByteCount('สวัสดี')).toBe(18);
});
});
17 changes: 17 additions & 0 deletions library/src/utils/_getByteCount/_getByteCount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
let textEncoder: TextEncoder;

/**
* Returns the byte count of the input.
*
* @param input The input to be measured.
*
* @returns The byte count.
*
* @internal
*/
export function _getByteCount(input: string): number {
if (!textEncoder) {
textEncoder = new TextEncoder();
}
return textEncoder.encode(input).length;
}
1 change: 1 addition & 0 deletions library/src/utils/_getByteCount/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './_getByteCount.ts';
1 change: 1 addition & 0 deletions library/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './_addIssue/index.ts';
export * from './_getByteCount/index.ts';
export * from './_getGraphemeCount/index.ts';
export * from './_isLuhnAlgo/index.ts';
export * from './_isValidObjectKey/index.ts';
Expand Down

0 comments on commit 1ecad75

Please sign in to comment.