Skip to content

Commit

Permalink
Added undefined type
Browse files Browse the repository at this point in the history
  • Loading branch information
Haaxor1689 committed Nov 26, 2023
1 parent 6496a84 commit c8a334e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,16 @@ const Level = n.enum(n.int8(), ['LOW', 'MEDIUM', 'HIGH']);
Level.options; // ["LOW", "MEDIUM", "HIGH"]
```

## Never
## Undefined

If you need a placeholder that represents 0 bytes in the binary data, you can use the never type for that:
If you need a placeholder that represents 0 bytes in the binary data, you can use the undefined type for that:

```ts
import { n } from '@haaxor1689/nil';

// Declare object schema with given shape
const User = n.object({
empty: n.never(), // represents 0 bytes in buffer
empty: n.undefined(), // represents 0 bytes in buffer
active: n.int32()
});

Expand All @@ -218,7 +218,7 @@ type User = n.output<typeof User>;

// Equivalent to
type User = {
empty: never;
empty: undefined;
active: boolean;
};
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@haaxor1689/nil",
"version": "0.1.1",
"version": "0.1.2",
"description": "TypeScript-first binary data parsing library with static type inference",
"author": "Maroš Beťko <[email protected]>",
"repository": {
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,13 +597,13 @@ export class NilEnum<
}
}

export class NilNever extends NilType<never> {
export class NilUndefined extends NilType<undefined> {
size() {
return 0;
}

_decode() {
return undefined as never;
return undefined;
}

_encode() {
Expand Down Expand Up @@ -636,7 +636,7 @@ const enum_ = <
type: T,
options: O
): NilEnum<T, O> => new NilEnum({ type, options });
const never = () => new NilNever({});
const undefined_ = () => new NilUndefined({});

export {
bool,
Expand All @@ -655,5 +655,5 @@ export {
object,
buffer,
enum_ as enum,
never
undefined_ as undefined
};

0 comments on commit c8a334e

Please sign in to comment.