Skip to content

Commit

Permalink
🌲: Annotate pure functions with @__NO_SIDE_EFFECTS__ to enable bett…
Browse files Browse the repository at this point in the history
…er tree-shaking. (#45)
  • Loading branch information
iwoplaza authored Jan 18, 2025
1 parent 9bca062 commit a7e3c8e
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/typed-binary/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "typed-binary",
"version": "4.3.1",
"sideEffects": false,
"description": "Describe binary structures with full TypeScript support. Encode and decode into pure JavaScript objects.",
"packageManager": "[email protected]+sha256.691fe176eea9a8a80df20e4976f3dfb44a04841ceb885638fe2a26174f81e65e",
"type": "module",
Expand Down
1 change: 1 addition & 0 deletions packages/typed-binary/src/io/unwrapBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface UnwrapBufferResult {
/**
* Removes up to one layer of view over a buffer.
*/
// @__NO_SIDE_EFFECTS__
export function unwrapBuffer(
buffer: ArrayBufferLike | ArrayBufferView,
): UnwrapBufferResult {
Expand Down
1 change: 1 addition & 0 deletions packages/typed-binary/src/structure/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class ArraySchema<TElement extends AnySchema> extends Schema<
}
}

// @__NO_SIDE_EFFECTS__
export function arrayOf<TSchema extends AnySchema>(
elementSchema: TSchema,
length: number,
Expand Down
1 change: 1 addition & 0 deletions packages/typed-binary/src/structure/chars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class CharsSchema<
}
}

// @__NO_SIDE_EFFECTS__
export function chars<T extends number>(length: T): CharsSchema<T> {
return new CharsSchema(length);
}
1 change: 1 addition & 0 deletions packages/typed-binary/src/structure/concat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Concat<Objs extends AnyObjectSchema[]> = ObjectSchema<
MergeRecordUnion<PropertiesOf<Objs[number]>>
>;

// @__NO_SIDE_EFFECTS__
export function concat<Objs extends AnyObjectSchema[]>(
objs: Objs,
): Concat<Objs> {
Expand Down
1 change: 1 addition & 0 deletions packages/typed-binary/src/structure/dynamicArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export class DynamicArraySchema<TElement extends AnySchema> extends Schema<
}
}

// @__NO_SIDE_EFFECTS__
export function dynamicArrayOf<TSchema extends AnySchema>(
elementSchema: TSchema,
): DynamicArraySchema<TSchema> {
Expand Down
1 change: 1 addition & 0 deletions packages/typed-binary/src/structure/keyed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export class KeyedSchema<
}
}

// @__NO_SIDE_EFFECTS__
export function keyed<K extends string, P extends ISchema<unknown>>(
key: K,
inner: (ref: ISchema<Ref<K>>) => P,
Expand Down
5 changes: 5 additions & 0 deletions packages/typed-binary/src/structure/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import {
type UnwrapRecord,
} from './types.ts';

// @__NO_SIDE_EFFECTS__
export function exactEntries<T extends Record<keyof T, T[keyof T]>>(
record: T,
): [keyof T, T[keyof T]][] {
return Object.entries(record) as [keyof T, T[keyof T]][];
}

// @__NO_SIDE_EFFECTS__
export function resolveMap<T extends Record<string, AnySchema>>(
ctx: IRefResolver,
refs: T,
Expand Down Expand Up @@ -134,6 +136,7 @@ export class ObjectSchema<TProps extends Record<string, AnySchema>>
}
}

// @__NO_SIDE_EFFECTS__
export function object<P extends Record<string, AnySchema>>(
properties: P,
): ObjectSchema<P> {
Expand Down Expand Up @@ -306,6 +309,7 @@ export class GenericObjectSchema<
}
}

// @__NO_SIDE_EFFECTS__
export function generic<
P extends Record<string, AnySchema>,
S extends {
Expand All @@ -315,6 +319,7 @@ export function generic<
return new GenericObjectSchema(SubTypeKey.STRING, properties, subTypeMap);
}

// @__NO_SIDE_EFFECTS__
export function genericEnum<
P extends Record<string, AnySchema>,
S extends {
Expand Down
1 change: 1 addition & 0 deletions packages/typed-binary/src/structure/optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class OptionalSchema<TInner extends AnySchema> extends Schema<
}
}

// @__NO_SIDE_EFFECTS__
export function optional<TSchema extends AnySchema>(
innerType: TSchema,
): OptionalSchema<TSchema> {
Expand Down
2 changes: 2 additions & 0 deletions packages/typed-binary/src/structure/tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
type UnwrapArray,
} from './types.ts';

// @__NO_SIDE_EFFECTS__
export function resolveArray<T extends AnySchema[]>(
ctx: IRefResolver,
refs: T,
Expand Down Expand Up @@ -88,6 +89,7 @@ export class TupleSchema<
}
}

// @__NO_SIDE_EFFECTS__
export function tupleOf<TSchema extends [AnySchema, ...AnySchema[]]>(
schemas: TSchema,
): TupleSchema<TSchema> {
Expand Down
9 changes: 9 additions & 0 deletions packages/typed-binary/src/structure/typedArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,40 @@ export class TypedArraySchema<
}
}

// @__NO_SIDE_EFFECTS__
export const u8Array = (length: number): TypedArraySchema<Uint8Array> =>
new TypedArraySchema(length, Uint8Array);

// @__NO_SIDE_EFFECTS__
export const u8ClampedArray = (
length: number,
): TypedArraySchema<Uint8ClampedArray> =>
new TypedArraySchema(length, Uint8ClampedArray);

// @__NO_SIDE_EFFECTS__
export const u16Array = (length: number): TypedArraySchema<Uint16Array> =>
new TypedArraySchema(length, Uint16Array);

// @__NO_SIDE_EFFECTS__
export const u32Array = (length: number): TypedArraySchema<Uint32Array> =>
new TypedArraySchema(length, Uint32Array);

// @__NO_SIDE_EFFECTS__
export const i8Array = (length: number): TypedArraySchema<Int8Array> =>
new TypedArraySchema(length, Int8Array);

// @__NO_SIDE_EFFECTS__
export const i16Array = (length: number): TypedArraySchema<Int16Array> =>
new TypedArraySchema(length, Int16Array);

// @__NO_SIDE_EFFECTS__
export const i32Array = (length: number): TypedArraySchema<Int32Array> =>
new TypedArraySchema(length, Int32Array);

// @__NO_SIDE_EFFECTS__
export const f32Array = (length: number): TypedArraySchema<Float32Array> =>
new TypedArraySchema(length, Float32Array);

// @__NO_SIDE_EFFECTS__
export const f64Array = (length: number): TypedArraySchema<Float64Array> =>
new TypedArraySchema(length, Float64Array);
2 changes: 2 additions & 0 deletions packages/typed-binary/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @returns {Boolean} true if system is big endian
*/
// @__NO_SIDE_EFFECTS__
function isSystemBigEndian(): boolean {
const array = new Uint8Array(4);
const view = new Uint32Array(array.buffer);
Expand All @@ -10,6 +11,7 @@ function isSystemBigEndian(): boolean {
return array[0] === 0; // if zero is the left-most byte, one was encoded as big endian
}

// @__NO_SIDE_EFFECTS__
export function getSystemEndianness(): 'big' | 'little' {
return isSystemBigEndian() ? 'big' : 'little';
}

0 comments on commit a7e3c8e

Please sign in to comment.