gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 2.49 kB
JavaScript
/*! Copyright 2023-2025 the gnablib contributors MPL-1.1 */
import{ColType as t}from"./ColType.js";import{ACudColType as n}from"./ACudColType.js";import{FromBinResult as e}from"../../primitive/FromBinResult.js";import{TypeProblem as i}from"../../error/probs/TypeProblem.js";import{RangeProblem as r}from"../../error/probs/RangeProblem.js";import{ContentError as s}from"../../error/ContentError.js";import{sLen as o}from"../../safe/safe.js";import{I64 as m}from"../../primitive/number/I64.js";class l extends n{constructor(t=!1){super(t)}cudByteSize(){return this._maxByteLen}valid(t){let n;if(null==t)return this.nullable?void 0:i.Null("Int");if(t instanceof m)n=t;else{if(!Number.isInteger(t))return i.UnexpVal("Int",t,"integer or I64");n=m.fromInt(t)}return n.lt(this._min64)||n.gt(this._max64)?r.IncInc("Int",t,this._min64,this._max64):void 0}unknownBin(t){let n;if(null==t){if(!this.nullable)throw new s("cannot be null","Int",void 0);return new Uint8Array([0])}if(t instanceof m)n=t;else{if("number"!=typeof t||!Number.isInteger(t))throw new TypeError("Integer or Int64 required");n=m.fromInt(t)}const e=n.toBytesBE();o("i64-bytes",e).atMost(this._maxByteLen).throwNot();const i=new Uint8Array(1+e.length);return i[0]=e.length,i.set(e,1),i}binUnknown(t,n){if(n+1>t.length)return new e(0,void 0,"Int.binUnknown unable to find length");const i=t[n++];if(0===i)return this.nullable?new e(1,void 0):new e(0,void 0,"Int.binUnknown cannot be null");if(i>this._maxByteLen)return new e(0,void 0,`Int.binUnknown invalid length (0<${this._maxByteLen} got ${i})`);return n+i>t.length?new e(0,void 0,"Int.binUnknown missing data"):new e(i+1,m.fromBytesBE(t,n))}}export class Int2 extends l{constructor(){super(...arguments),this._colType=t.Int2,this._maxByteLen=8,this._min64=m.fromI32s(4294934528,4294967295),this._max64=m.fromI32s(32767,0),this.mysqlType="SMALLINT",this.sqliteType="INT2",this.postgresType="smallint",this.cudType="int2"}}export class Int4 extends l{constructor(){super(...arguments),this._colType=t.Int4,this._maxByteLen=8,this._min64=m.fromI32s(2147483648,4294967295),this._max64=m.fromI32s(2147483647,0),this.mysqlType="INT",this.sqliteType="INT",this.postgresType="int",this.cudType="int4"}}export class Int8 extends l{constructor(){super(...arguments),this._colType=t.Int8,this._maxByteLen=8,this._min64=m.fromI32s(0,2147483648),this._max64=m.fromI32s(4294967295,34359738367),this.mysqlType="BIGINT",this.sqliteType="INT",this.postgresType="bigint",this.cudType="int8"}}