Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ export const examples: {
readonly BloomFilter: 'examples/bloomFilter.ts';
readonly SegmentTree: 'examples/segmentTree.ts';
readonly SkipList: 'examples/skipList.ts';
readonly runLengthEncode: 'examples/rle.ts';
readonly runLengthDecode: 'examples/rle.ts';
readonly base64Encode: 'examples/base64.ts';
readonly base64Decode: 'examples/base64.ts';
};
readonly performance: {
readonly debounce: 'examples/requestDedup.ts';
Expand Down Expand Up @@ -2998,6 +3002,23 @@ export class SkipList<T> {
values(): IterableIterator<T>;
}

/**
* Run-length encoding for strings.
* Use for: simple compression of repetitive text.
* Import: data/rle.ts
*/
export interface RlePair { char: string; count: number }
export function runLengthEncode(input: string): RlePair[];
export function runLengthDecode(pairs: ReadonlyArray<RlePair>): string;

/**
* Base64 encode/decode utilities (UTF-8 strings and bytes).
* Use for: compact textual transport, data URIs, wire formats.
* Import: data/base64.ts
*/
export function base64Encode(input: string | Uint8Array): string;
export function base64Decode(b64: string): Uint8Array;

/**
* Disjoint Set Union (Union-Find) with path compression and union by size.
* Use for: connectivity queries, Kruskal MST, clustering.
Expand Down
7 changes: 7 additions & 0 deletions examples/base64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { base64Encode, base64Decode } from '../src/index.js';

const s = 'Hello, 世界! 🎉';
const b64 = base64Encode(s);
console.log('b64:', b64);
console.log('utf8:', new TextDecoder().decode(base64Decode(b64)));

7 changes: 7 additions & 0 deletions examples/rle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { runLengthEncode, runLengthDecode } from '../src/index.js';

const s = 'AAAABBBCCDAA';
const pairs = runLengthEncode(s);
console.log('pairs', pairs);
console.log('decoded', runLengthDecode(pairs));

41 changes: 41 additions & 0 deletions src/data/base64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Base64 encode/decode utilities (UTF-8 strings and bytes).
*/
export function base64Encode(input: string | Uint8Array): string {
if (typeof input === 'string') {
const bytes = new TextEncoder().encode(input);
return encodeBytes(bytes);
}
return encodeBytes(input);
}

export function base64Decode(b64: string): Uint8Array {
return decodeToBytes(b64);
}

function encodeBytes(bytes: Uint8Array): string {
if (typeof Buffer !== 'undefined') {
// Node
// eslint-disable-next-line no-undef
return Buffer.from(bytes).toString('base64');
}
// Browser
let binary = '';
for (let i = 0; i < bytes.length; i += 1) binary += String.fromCharCode(bytes[i]);
const g = globalThis as unknown as { btoa?: (s: string) => string };
if (!g.btoa) throw new Error('Base64 encode not available in this environment');
return g.btoa(binary);
}

function decodeToBytes(b64: string): Uint8Array {
if (typeof Buffer !== 'undefined') {
// eslint-disable-next-line no-undef
return new Uint8Array(Buffer.from(b64, 'base64'));
}
const g = globalThis as unknown as { atob?: (s: string) => string };
if (!g.atob) throw new Error('Base64 decode not available in this environment');
const binary: string = g.atob(b64);
const out = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i += 1) out[i] = binary.charCodeAt(i);
return out;
}
42 changes: 42 additions & 0 deletions src/data/rle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Run-length encoding for strings.
* Useful for: simple compression on repetitive text.
*/
export interface RlePair {
char: string;
count: number;
}

/**
* Encodes a string into RLE pairs.
*/
export function runLengthEncode(input: string): RlePair[] {
if (input.length === 0) return [];
const out: RlePair[] = [];
let last = input[0];
let cnt = 1;
for (let i = 1; i < input.length; i += 1) {
const c = input[i];
if (c === last) cnt += 1;
else {
out.push({ char: last, count: cnt });
last = c;
cnt = 1;
}
}
out.push({ char: last, count: cnt });
return out;
}

/**
* Decodes RLE pairs into a string.
*/
export function runLengthDecode(pairs: ReadonlyArray<RlePair>): string {
let out = '';
for (const p of pairs) {
if (!p || typeof p.count !== 'number' || typeof p.char !== 'string') continue;
if (p.count <= 0 || p.char.length === 0) continue;
out += p.char.repeat(p.count);
}
return out;
}
16 changes: 16 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ export const examples = {
BloomFilter: 'examples/bloomFilter.ts',
SegmentTree: 'examples/segmentTree.ts',
SkipList: 'examples/skipList.ts',
runLengthEncode: 'examples/rle.ts',
runLengthDecode: 'examples/rle.ts',
base64Encode: 'examples/base64.ts',
base64Decode: 'examples/base64.ts',
},
performance: {
debounce: 'examples/requestDedup.ts',
Expand Down Expand Up @@ -1044,6 +1048,18 @@ export { BinaryHeap } from './data/binaryHeap.js';
* Example file: examples/skipList.ts
*/
export { SkipList } from './data/skipList.js';
/**
* Run-length encoding helpers for strings.
*
* Example file: examples/rle.ts
*/
export { runLengthEncode, runLengthDecode } from './data/rle.js';
/**
* Base64 encode/decode helpers for strings and bytes.
*
* Example file: examples/base64.ts
*/
export { base64Encode, base64Decode } from './data/base64.js';

export type {
TreeNode,
Expand Down
13 changes: 13 additions & 0 deletions tests/base64.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { describe, it, expect } from 'vitest';
import { base64Encode, base64Decode } from '../src/index.js';

describe('Base64', () => {
it('encodes and decodes UTF-8 strings', () => {
const s = 'Hello, 世界! 🎉';
const b64 = base64Encode(s);
const bytes = base64Decode(b64);
const back = new TextDecoder().decode(bytes);
expect(back).toBe(s);
});
});

4 changes: 4 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ describe('package entry point', () => {
| 'BloomFilter'
| 'SegmentTree'
| 'SkipList'
| 'runLengthEncode'
| 'runLengthDecode'
| 'base64Encode'
| 'base64Decode'
>();

expectTypeOf<ExampleName<'search'>>().toEqualTypeOf<
Expand Down
24 changes: 24 additions & 0 deletions tests/rle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, it, expect } from 'vitest';
import { runLengthEncode, runLengthDecode } from '../src/index.js';

describe('RLE', () => {
it('encodes and decodes repetitive strings', () => {
const s = 'AAAABBBCCDAA';
const pairs = runLengthEncode(s);
expect(pairs).toEqual([
{ char: 'A', count: 4 },
{ char: 'B', count: 3 },
{ char: 'C', count: 2 },
{ char: 'D', count: 1 },
{ char: 'A', count: 2 },
]);
expect(runLengthDecode(pairs)).toBe(s);
});

it('handles empty and mixed content', () => {
expect(runLengthEncode('')).toEqual([]);
const s = 'ABAB';
expect(runLengthDecode(runLengthEncode(s))).toBe(s);
});
});