Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added int/uint types #681

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,28 @@ export const WithUnion: S.WithUnion1<URI> = {
export const WithRefine: S.WithRefine1<URI> = {
refine: refine as S.WithRefine1<URI>['refine']
}

export const int = (int: number): t.Type<number, number, unknown> => {
const i = BigInt(int-1);
const two = BigInt(2)
const max = BigInt((((two<<i)/two)-BigInt(1)));
const min = -BigInt(((two<<(i))/two));
return new t.Type<number>(
'int',
t.number.is,
(s, context) => (typeof s === 'number' && Number.isInteger(s) && BigInt(s) >= min && BigInt(s) <= max ? t.success(s) : t.failure(s, context)),
t.identity,
);
}

export const uint = (int: number): t.Type<number, number, unknown> => {
const i = BigInt(int-1);
const two = BigInt(2)
const max = BigInt((((two<<i))-BigInt(1)));
return new t.Type<number>(
'uint',
t.number.is,
(s, context) => (typeof s === 'number' && Number.isInteger(s) && s >= 0 && BigInt(s) <= max ? t.success(s) : t.failure(s, context)),
t.identity,
);
}
65 changes: 65 additions & 0 deletions test/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,69 @@ describe('Type', () => {
assert.deepStrictEqual(isRight(type.decode('a')), true)
assert.deepStrictEqual(isRight(type.decode('')), false)
})
it('Success int8', () => {
const tint8 = _.int(8)
const int8: G.TypeOf<typeof tint8> = 127;
assert.equal(tint8.validate(int8, [])._tag, 'Right')
})
it('Not success int8', () => {
const tint8 = _.int(8)
const int8: G.TypeOf<typeof tint8> = 128;
assert.equal(tint8.validate(int8, [])._tag, 'Left')
})
it('Success uint8', () => {
const tuint8 = _.uint(8)
const uint8: G.TypeOf<typeof tuint8> = 255;
assert.equal(tuint8.validate(uint8, [])._tag, 'Right')
})
it('Not success uint8', () => {
const tuint8 = _.uint(8)
const uint8: G.TypeOf<typeof tuint8> = 256;
assert.equal(tuint8.validate(uint8, [])._tag, 'Left')
})
it('Success int16', () => {
const tint16 = _.int(16)
const int16: G.TypeOf<typeof tint16> = 32767;
assert.equal(tint16.validate(int16, [])._tag, 'Right')
})
it('Not success int16', () => {
const tint16 = _.int(16)
const int16: G.TypeOf<typeof tint16> = 32768;
assert.equal(tint16.validate(int16, [])._tag, 'Left')
})
it('Success uint16', () => {
const tuint16 = _.uint(16)
const uint16: G.TypeOf<typeof tuint16> = 65535;
assert.equal(tuint16.validate(uint16, [])._tag, 'Right')
})
it('Not success uint16', () => {
const tuint16 = _.uint(16)
const uint16: G.TypeOf<typeof tuint16> = 65536;
assert.equal(tuint16.validate(uint16, [])._tag, 'Left')
})
it('Success int32', () => {
const tint32 = _.int(32)
const int32: G.TypeOf<typeof tint32> = 2147483647;
assert.equal(tint32.validate(int32, [])._tag, 'Right')
})
it('Not success int32', () => {
const tint32 = _.int(32)
const int32: G.TypeOf<typeof tint32> = 2147483648;
assert.equal(tint32.validate(int32, [])._tag, 'Left')
})
it('Success uint32', () => {
const tuint32 = _.uint(32)
const uint32: G.TypeOf<typeof tuint32> = 4294967295;
assert.equal(tuint32.validate(uint32, [])._tag, 'Right')
})
it('Not success uint32', () => {
const tuint32 = _.uint(32)
const uint32: G.TypeOf<typeof tuint32> = 4294967296;
assert.equal(tuint32.validate(uint32, [])._tag, 'Left')
})
it('Not success uint32 (int)', () => {
const tuint32 = _.uint(32)
const uint32: G.TypeOf<typeof tuint32> = 4.5;
assert.equal(tuint32.validate(uint32, [])._tag, 'Left')
})
})