New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

binaryarray

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

binaryarray - npm Package Compare versions

Comparing version
0.1.2
to
0.1.3
+23
dist/lib/bitsize_util.d.ts
/**
* Returns the bit size defined by this module.
* @returns bit size
*/
export declare const getBitSize: () => number;
/**
* Calculate the array length from the number of flags
* @param flagmax max bits
* @returns array length
*/
export declare const getArraySize: (flagmax: number) => number;
/**
* Find the position of the array from the flag number
* @param no flag number
* @returns array index
*/
export declare const getArrayIndex: (no: number) => number;
/**
* Find the bit position from the flag number
* @param no flag number
* @returns bit index
*/
export declare const getFlagPos: (no: number) => number;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// Number of bit
var BITS = 32;
/**
* Returns the bit size defined by this module.
* @returns bit size
*/
exports.getBitSize = function () { return BITS; };
/**
* Calculate the array length from the number of flags
* @param flagmax max bits
* @returns array length
*/
exports.getArraySize = function (flagmax) { return Math.ceil(flagmax / BITS); };
/**
* Find the position of the array from the flag number
* @param no flag number
* @returns array index
*/
exports.getArrayIndex = function (no) { return Math.floor(no / BITS); };
/**
* Find the bit position from the flag number
* @param no flag number
* @returns bit index
*/
exports.getFlagPos = function (no) { return no % BITS; };
// Number of bit
const BITS : number = 32;
/**
* Returns the bit size defined by this module.
* @returns bit size
*/
export const getBitSize = () : number => BITS
/**
* Calculate the array length from the number of flags
* @param flagmax max bits
* @returns array length
*/
export const getArraySize = (flagmax : number) : number => Math.ceil(flagmax / BITS)
/**
* Find the position of the array from the flag number
* @param no flag number
* @returns array index
*/
export const getArrayIndex = (no : number) : number => Math.floor(no / BITS)
/**
* Find the bit position from the flag number
* @param no flag number
* @returns bit index
*/
export const getFlagPos = (no : number) : number => no % BITS
+3
-2

@@ -5,2 +5,3 @@ export declare class BinaryArray {

constructor(maxnum: number);
private findBitPosition(no);
bitOn(no: number): BinaryArray;

@@ -13,3 +14,3 @@ bitOff(no: number): BinaryArray;

isRange(no: number): boolean;
rangeOf(xs: any): Object;
rangeOf(no_list: number | Array<number>): Object;
check(on_list: Array<number>, off_list?: Array<number>): boolean;

@@ -19,4 +20,4 @@ toHexString(): string;

static loadFromHexString(maxnum: number, str: string): BinaryArray;
static loadFromArray(xs: Array<number>): BinaryArray;
static loadFromArray(flaglist: Array<number>): BinaryArray;
static deserialize(list: Array<string>, spec: Object, max: number): BinaryArray;
}
+30
-38
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var assert = require("assert");
var util = require("./util");
var BinaryArray = (function () {
var util_1 = require("./util");
var bitsize_util_1 = require("./bitsize_util");
var BinaryArray = /** @class */ (function () {
function BinaryArray(maxnum) {
this.maxnum = maxnum;
this.storage = util.createArray(util.getArraySize(maxnum), 0);
this.storage = util_1.createArray(bitsize_util_1.getArraySize(maxnum), 0);
}
BinaryArray.prototype.bitOn = function (no) {
BinaryArray.prototype.findBitPosition = function (no) {
assert(this.maxnum > no, 'on:over flagmax');
var idx = util.getArrayIndex(no);
var idx = bitsize_util_1.getArrayIndex(no);
assert(idx >= 0 && idx < this.storage.length, 'on:over idx range');
var pos = util.getFlagPos(no);
var pos = bitsize_util_1.getFlagPos(no);
var flag = 1 << pos;
return [idx, flag];
};
BinaryArray.prototype.bitOn = function (no) {
var _a = this.findBitPosition(no), idx = _a[0], flag = _a[1];
this.storage[idx] |= flag;

@@ -20,7 +25,3 @@ return this;

BinaryArray.prototype.bitOff = function (no) {
assert(this.maxnum > no, 'off:over flagmax');
var idx = util.getArrayIndex(no);
assert(idx >= 0 && idx < this.storage.length, 'off:over idx range');
var pos = util.getFlagPos(no);
var flag = 1 << pos;
var _a = this.findBitPosition(no), idx = _a[0], flag = _a[1];
if (this.storage[idx] & flag) {

@@ -32,22 +33,15 @@ this.storage[idx] ^= flag;

BinaryArray.prototype.at = function (no) {
assert(this.maxnum > no, 'get:over flagmax');
var idx = util.getArrayIndex(no);
assert(idx >= 0 && idx < this.storage.length, 'is:over idx range');
var pos = util.getFlagPos(no);
var flag = 1 << pos;
var _a = this.findBitPosition(no), idx = _a[0], flag = _a[1];
return this.storage[idx] & flag ? 1 : 0;
};
BinaryArray.prototype.toArray = function () {
var w = [];
var max = this.maxnum;
for (var i = 0; i < max; ++i) {
w.push(this.at(i));
}
return w;
var _this = this;
var results = Array.from(Array(this.maxnum), function (v, idx) { return _this.at(idx); });
return results;
};
BinaryArray.prototype.serialize = function (spec) {
assert(spec instanceof Object, 'spec is must be Object');
var w = this.toArray();
var flaglist = this.toArray();
return Object.keys(spec)
.filter(function (k) { return w[spec[k]]; });
.filter(function (k) { return flaglist[spec[k]]; });
};

@@ -61,3 +55,3 @@ BinaryArray.prototype.toJSON = function () {

}
var idx = util.getArrayIndex(no);
var idx = bitsize_util_1.getArrayIndex(no);
if (!(idx >= 0 && idx < this.storage.length)) {

@@ -68,9 +62,7 @@ return false;

};
BinaryArray.prototype.rangeOf = function (xs) {
BinaryArray.prototype.rangeOf = function (no_list) {
var _this = this;
if (!(xs instanceof Array)) {
xs = [xs];
}
return xs.reduce(function (r, v) {
r[v] = _this.at(v);
var xs = (no_list instanceof Array) ? no_list : [no_list];
return xs.reduce(function (r, no) {
r[no] = _this.at(no);
return r;

@@ -91,3 +83,3 @@ }, {});

for (var i = n - 1; i >= 0; --i) {
str = str + util.NumberToHexString(this.storage[i], 8);
str = str + util_1.numberToHexString(this.storage[i], 8);
}

@@ -112,8 +104,8 @@ return str;

};
BinaryArray.loadFromArray = function (xs) {
var ba = new BinaryArray(xs.length);
xs.map(function (v, i) { return [v, i]; })
.filter(function (v) { return v[0]; })
.forEach(function (v) {
ba.bitOn(v[1]);
BinaryArray.loadFromArray = function (flaglist) {
var ba = new BinaryArray(flaglist.length);
flaglist.map(function (v, i) { return [i, v]; })
.filter(function (tuple) { return tuple[1]; })
.forEach(function (tuple) {
ba.bitOn(tuple[0]);
});

@@ -120,0 +112,0 @@ return ba;

@@ -1,8 +0,20 @@

export declare const getArraySize: (flagmax: number) => number;
export declare const getArrayIndex: (no: number) => number;
export declare const getFlagPos: (no: number) => number;
export declare const digitfix: (str: string, b: number) => string;
export declare const hexconv: (n: number) => string;
export declare const NumberToHexString: (num: number, digit: number) => string;
/**
* Make it a number from hex string
* @param num
* @param digit
* @returns hexstring
*/
export declare const numberToHexString: (num: number, digit: number) => string;
/**
* Create an initialized typed-array
* @param size array length
* @param init_val initialize value
* @returns initialized typed-arrray
*/
export declare const createArray: (size: number, init_val?: number) => Uint32Array;
/**
* max value of object propaties
* @param spec enum-like Object
* @returns max value
*/
export declare const getSpecMax: (spec: Object) => number;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var BITS = 32;
exports.getArraySize = function (flagmax) {
return Math.ceil(flagmax / BITS);
/**
* Make it a number from hex string
* @param num
* @param digit
* @returns hexstring
*/
exports.numberToHexString = function (num, digit) {
var template = Array.from(Array(digit), function () { return 0; }).join('');
var hexstring = num.toString(16).toUpperCase();
return (template + hexstring).slice(-digit);
};
exports.getArrayIndex = function (no) {
return Math.floor(no / BITS);
};
exports.getFlagPos = function (no) {
return no % BITS;
};
exports.digitfix = function (str, b) {
if (str.length < b) {
var len = b - str.length;
for (var i = 0; i < len; ++i) {
str = '0' + str;
}
}
return str;
};
exports.hexconv = function (n) {
switch (n) {
case 10: return 'A';
case 11: return 'B';
case 12: return 'C';
case 13: return 'D';
case 14: return 'E';
case 15: return 'F';
}
return n.toString();
};
exports.NumberToHexString = function (num, digit) {
var remainder = 0;
var str = '';
while (num > 0) {
remainder = num % 16;
num = (num - remainder) / 16;
str = exports.hexconv(remainder) + str;
}
return exports.digitfix(str, digit);
};
/**
* Create an initialized typed-array
* @param size array length
* @param init_val initialize value
* @returns initialized typed-arrray
*/
exports.createArray = function (size, init_val) {

@@ -51,4 +28,9 @@ if (init_val === void 0) { init_val = 0; }

};
/**
* max value of object propaties
* @param spec enum-like Object
* @returns max value
*/
exports.getSpecMax = function (spec) {
return Object.keys(spec).reduce(function (r, k) { return Math.max(r, spec[k]); }, 0) + 1;
};

@@ -6,2 +6,3 @@ "use strict";

var util_1 = require("../lib/util");
var bitsize_util_1 = require("../lib/bitsize_util");
describe('test', function () {

@@ -240,17 +241,18 @@ it('array length test', function () {

},
/* todo typescript type
()=>{
let flag = 1;
const ba = new BinaryArray(1024);
ba.bitOn(3);
try{
const obj = ba.rangeOf("AAA");
}catch(e){
flag = 0;
}
assert(flag === 0);
},
*/
function () {
var flag = 1;
var ba = new binaryarray_1.BinaryArray(1024);
ba.bitOn(3);
try {
var obj = ba.rangeOf("AAA");
}
catch (e) {
flag = 0;
}
assert(flag === 0);
},
function () {
var ba = new binaryarray_1.BinaryArray(1024);
ba.bitOn(3);
var ba2 = ba.clone();

@@ -260,2 +262,5 @@ assert(ba2.at(3));

function () {
assert(bitsize_util_1.getBitSize() === 32);
},
function () {
var EVENT_CLEAR;

@@ -262,0 +267,0 @@ (function (EVENT_CLEAR) {

{
"name": "binaryarray",
"version": "0.1.2",
"version": "0.1.3",
"description": "the binary array",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -1,52 +0,47 @@

import * as assert from 'assert';
import * as util from './util';
import * as assert from 'assert'
import { numberToHexString, createArray, getSpecMax } from './util'
import { getArraySize, getArrayIndex, getFlagPos } from './bitsize_util'
type TupleFlag = [number, number]
export class BinaryArray{
readonly maxnum: number;
readonly storage: Uint32Array;
readonly maxnum: number
readonly storage: Uint32Array
constructor(maxnum : number){
this.maxnum = maxnum
this.storage = util.createArray(util.getArraySize(maxnum), 0)
this.storage = createArray(getArraySize(maxnum), 0)
}
private findBitPosition(no : number) : TupleFlag {
assert(this.maxnum > no, 'on:over flagmax')
const idx = getArrayIndex(no)
assert(idx >= 0 && idx < this.storage.length, 'on:over idx range')
const pos = getFlagPos(no)
const flag = 1 << pos
return [idx, flag]
}
bitOn(no : number) : BinaryArray{
assert(this.maxnum > no, 'on:over flagmax');
const idx = util.getArrayIndex(no);
assert(idx >= 0 && idx < this.storage.length, 'on:over idx range');
const pos = util.getFlagPos(no);
const flag = 1 << pos;
this.storage[idx] |= flag;
return this;
const [idx, flag] = this.findBitPosition(no)
this.storage[idx] |= flag
return this
}
bitOff(no : number) : BinaryArray{
assert(this.maxnum > no, 'off:over flagmax');
const idx = util.getArrayIndex(no);
assert(idx >= 0 && idx < this.storage.length, 'off:over idx range');
const pos = util.getFlagPos(no);
const flag = 1 << pos;
const [idx, flag] = this.findBitPosition(no)
if(this.storage[idx] & flag){
this.storage[idx] ^= flag;
this.storage[idx] ^= flag
}
return this;
return this
}
at(no : number) : number{
assert(this.maxnum > no, 'get:over flagmax');
const idx = util.getArrayIndex(no);
assert(idx >= 0 && idx < this.storage.length, 'is:over idx range');
const pos = util.getFlagPos(no);
const flag = 1 << pos;
return this.storage[idx] & flag ? 1 : 0;
const [idx, flag] = this.findBitPosition(no)
return this.storage[idx] & flag ? 1 : 0
}
toArray() : Array<number>{
const w : Array<number> = [];
const max = this.maxnum;
for(let i = 0; i < max; ++i ){
w.push(this.at(i));
}
return w;
const results : Array<number> = Array.from( Array(this.maxnum), (v, idx) => this.at(idx))
return results
}
serialize(spec : Object) : Array<string>{
assert(spec instanceof Object, 'spec is must be Object')
const w = this.toArray();
const flaglist = this.toArray()
return Object.keys(spec)
.filter((k) => w[spec[k]])
.filter((k) => flaglist[spec[k]])
}

@@ -58,22 +53,20 @@ toJSON() : string{

if(!(this.maxnum > no)){
return false;
return false
}
const idx = util.getArrayIndex(no);
const idx = getArrayIndex(no)
if(!(idx >= 0 && idx < this.storage.length)){
return false;
return false
}
return true;
return true
}
rangeOf(xs : any) : Object{
if(!(xs instanceof Array)){
xs = [xs];
}
return xs.reduce((r, v) => {
r[v] = this.at(v)
return r;
rangeOf(no_list : number | Array<number>) : Object{
const xs = (no_list instanceof Array) ? no_list : [no_list]
return xs.reduce((r : Object, no : number) => {
r[no] = this.at(no)
return r
}, {})
}
check(on_list : Array<number>, off_list : Array<number> = []) : boolean{
const on = this.rangeOf(on_list);
const off = this.rangeOf(off_list);
const on = this.rangeOf(on_list)
const off = this.rangeOf(off_list)
const x = Object.keys(on).reduce((r, v) => { return r & on[v] }, 1)

@@ -84,39 +77,39 @@ const y = Object.keys(off).reduce((r, v) => { return r & ~off[v] }, 1)

toHexString() : string{
let str = '';
const n = this.storage.length;
let str = ''
const n = this.storage.length
for(let i = n - 1; i >= 0; --i){
str = str + util.NumberToHexString(this.storage[i], 8);
str = str + numberToHexString(this.storage[i], 8)
}
return str;
return str
}
clone() : BinaryArray{
return BinaryArray.loadFromArray(this.toArray());
return BinaryArray.loadFromArray(this.toArray())
}
static loadFromHexString(maxnum : number, str : string) : BinaryArray{
const ba = new BinaryArray(maxnum);
const s = 8;
let b = str.length - s;
let e = str.length - 0;
const n = str.length / s;
const ba = new BinaryArray(maxnum)
const s = 8
let b = str.length - s
let e = str.length - 0
const n = str.length / s
for(let i = 0; i < n; ++i){
ba.storage[i] = parseInt(str.substring(b,e), 16);
b -= s;
e -= s;
ba.storage[i] = parseInt(str.substring(b, e), 16)
b -= s
e -= s
}
return ba;
return ba
}
static loadFromArray(xs : Array<number>) : BinaryArray{
const ba = new BinaryArray(xs.length);
xs.map((v, i) => [v, i])
.filter((v) => v[0])
.forEach((v) => {
ba.bitOn(v[1]);
static loadFromArray(flaglist : Array<number>) : BinaryArray{
const ba = new BinaryArray(flaglist.length)
flaglist.map((v : number, i : number) : TupleFlag => [i, v])
.filter((tuple) => tuple[1])
.forEach((tuple) => {
ba.bitOn(tuple[0])
})
return ba;
return ba
}
static deserialize(list : Array<string>, spec : Object, max : number) : BinaryArray{
const ba = new BinaryArray(max);
const ba = new BinaryArray(max)
list.forEach((v) => { ba.bitOn(spec[v]) })
return ba;
return ba
}
}

@@ -1,56 +0,19 @@

// 配列一つに入るビットの数
const BITS : number = 32;
// フラグ数から配列のサイズを求める
export const getArraySize = (flagmax : number) : number => {
return Math.ceil(flagmax / BITS);
/**
* Make it a number from hex string
* @param num
* @param digit
* @returns hexstring
*/
export const numberToHexString = (num : number, digit : number) : string => {
const template = Array.from(Array(digit), () => 0).join('')
const hexstring = num.toString(16).toUpperCase()
return (template + hexstring).slice(-digit)
}
// フラグ番号から配列の位置を求める
export const getArrayIndex = (no : number) : number => {
return Math.floor(no / BITS);
}
// フラグ番号から実際のフラグの位置を求める
export const getFlagPos = (no : number) : number => {
return no % BITS;
}
export const digitfix = (str : string, b : number) : string => {
if(str.length < b ){
const len = b - str.length;
for(let i = 0; i < len; ++i){
str = '0' + str;
}
}
return str;
};
export const hexconv = (n : number) : string => {
switch (n) {
case 10: return 'A';
case 11: return 'B';
case 12: return 'C';
case 13: return 'D';
case 14: return 'E';
case 15: return 'F';
}
return n.toString();
};
// 数値からヘックスの文字列にする
export const NumberToHexString = (num : number, digit : number) : string => {
let remainder = 0;
let str = '';
while (num > 0) {
remainder = num % 16;
num = (num - remainder) / 16;
str = hexconv(remainder) + str;
}
return digitfix(str, digit);
}
// 初期化済みの配列を作成する
/**
* Create an initialized typed-array
* @param size array length
* @param init_val initialize value
* @returns initialized typed-arrray
*/
export const createArray = (size : number, init_val : number = 0) : Uint32Array => {

@@ -64,2 +27,7 @@ const w = new Uint32Array(size);

/**
* max value of object propaties
* @param spec enum-like Object
* @returns max value
*/
export const getSpecMax = (spec : Object) : number => {

@@ -69,2 +37,1 @@ return Object.keys(spec).reduce((r, k) => Math.max(r, spec[k]), 0) + 1

import * as assert from 'assert'
import {BinaryArray} from '../lib/binaryarray'
import {getSpecMax} from "../lib/util"
import {getBitSize} from "../lib/bitsize_util"

@@ -230,2 +231,3 @@ describe('test', () => {

},
/* todo typescript type
()=>{

@@ -242,2 +244,3 @@ let flag = 1;

},
*/
()=>{

@@ -250,2 +253,5 @@ const ba = new BinaryArray(1024);

()=>{
assert(getBitSize() === 32)
},
()=>{
enum EVENT_CLEAR {

@@ -252,0 +258,0 @@ TUTORIAL,