binary-utility-functions
Version:
This library provides a handy set of utility functions for manipulating binary values- such as those ascertained from a Buffer, or a TCP stream. These functions can be split into two categories: 1) Type validity checking 2) Type conversion
4 lines (2 loc) • 3.56 kB
JavaScript
var I=Object.defineProperty;var t=(r,e)=>I(r,"name",{value:e,configurable:!0});function _(r){return !!(Number.isInteger(r)&&r<=15&&r>=0)}t(_,"is_valid_nibble");function o(r){return !!(Number.isInteger(r)&&r<=255&&r>=0)}t(o,"is_valid_u8");function u(r){return !!(Number.isInteger(r)&&r<=65535&&r>=0)}t(u,"is_valid_u16");function i(r){return !!(Number.isInteger(r)&&r<=4294967295&&r>=0)}t(i,"is_valid_u32");function s(r,e){if(!_(r)||!_(e))throw new Error("util::two_nibble_to_one_byte::is_valid_nibble::false");return r<<4|e}t(s,"two_nibble_to_one_u8");function l(r){if(!o(r))throw new Error("util::one_u8_to_two_nibbles::is_valid_u8::false");return [r>>4,r&15]}t(l,"one_u8_to_two_nibbles");function m(r,e){if(!o(r)||!o(e))throw new Error("util::two_u8_to_one_u16::is_valid_u8::false");return r<<8|e}t(m,"two_u8_to_one_u16");function f(r,e,n){if(!o(r)||!o(e)||!o(n))throw new Error("util::three_u8_to_u32::is_valid_u8::false");return r<<16|e<<8|n}t(f,"three_u8_to_one_u32");function d(r,e,n,b){if(!o(r)||!o(e)||!o(n)||!o(b))throw new Error("util::four_u8_to_one_u32::is_valid_u8::false");return r<<24|e<<16|n<<8|b}t(d,"four_u8_to_one_u32");function w(r){if(!u(r))throw new Error("util::one_u16_to_two_u8::is_valid_u16::false");return [r>>8,r&255]}t(w,"one_u16_to_two_u8");function p(r,e){if(!u(r)||!u(e))throw new Error("util::two_u16_to_one_u32::is_valid_u16::false");return (r<<16|e)>>>0}t(p,"two_u16_to_one_u32");function x(r){if(!i(r))throw new Error("util::one_u32_to_two_u16::is_valid_u32::false");return [r>>>16,r&65535]}t(x,"one_u32_to_two_u16");function h(r){if(!i(r))throw new Error("util::one_u32_to_three_u8::is_valid_u32::false");return [r>>>24,r>>>16&255,r>>>8&255,r&255]}t(h,"one_u32_to_four_u8");var v={nx2_u8x1:s,u8x1_nx2:l,u8x2_u16x1:m,u8x3_u32x1:f,u8x4_u32x1:d,u16x1_u8x2:w,u16x2_u32x1:p,u32x1_u16x2:x,u32x1_u8x4:h};var a=class{constructor(e){this.lower_bound=0;this.higher_bound=7;this.valid_handler=e=>!1;switch(e){case"nibble":this.higher_bound=3,this.valid_handler=_;break;case"u8":this.higher_bound=7,this.valid_handler=o;break;case"u16":this.higher_bound=15,this.valid_handler=u;break;case"u32":this.higher_bound=31,this.valid_handler=i;break;default:throw new Error(`util::DataType::Type::${e} Unrecognized`)}this.type_name=e;}validate(e,n){for(let b of arguments)if(isNaN(b)||typeof b!="number")throw new Error(`util::DataType::${this.type_name}_bit_set::validate::InvalidParameter`);return !(!this.valid_handler(e)||n<this.lower_bound||n>this.higher_bound)}is_bit_set(e,n){if(!this.validate(e,n))throw new Error(`util::DataType::${this.type_name}_bit_set::ParamOutOfBounds`);return (e>>n&1)!=0}};t(a,"DataType");function c(r,e){for(let n of arguments)if(isNaN(n)||typeof n!="number")throw new Error("util::DataType::nibble_bit_set::InvalidParameters");return new a("nibble").is_bit_set(r,e)}t(c,"nibble_bit_set");function F(r,e){for(let n of arguments)if(isNaN(n)||typeof n!="number")throw new Error("util::DataType::u8_bit_set::InvalidParameters");return new a("u8").is_bit_set(r,e)}t(F,"u8_bit_set");function E(r,e){for(let n of arguments)if(isNaN(n)||typeof n!="number")throw new Error("util::DataType::u16_bit_set::InvalidParameters");return new a("u16").is_bit_set(r,e)}t(E,"u16_bit_set");function N(r,e){for(let n of arguments)if(isNaN(n)||typeof n!="number")throw new Error("util::DataType::u32_bit_set::InvalidParameters");return new a("u32").is_bit_set(r,e)}t(N,"u32_bit_set");var g={is_bit_set:{nibble:c,u8:F,u16:E,u32:N}};var Q={nibble:_,u8:o,u16:u,u32:i},R={...v},S={...g};
export { S as bits, R as convert, Q as validate };