Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

asclasit

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

asclasit - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

func2/cache.js

2

.vscode/launch.json

@@ -36,3 +36,3 @@ {

"func2/class."
"func2/cache."
],

@@ -39,0 +39,0 @@ "console": "integratedTerminal",

@@ -86,2 +86,82 @@ class Deque {

locate(index) {
if (index < 0) {
let cur = this._last;
while (cur) {
index += cur.chunk.length;
if (index >= cur.index) return {cur, index};
index -= cur.index;
cur = cur.prev;
}
return {cur, index};
} else {
let cur = this._first;
while (cur) {
index += cur.index;
if (index < cur.chunk.length) return {cur, index};
index -= cur.chunk.length;
cur = cur.next;
}
return {cur, index};
}
}
get(idx) {
const {cur, index} = this.locate(idx);
if (!cur) return;
return cur.chunk[index];
}
get second() {
return this.get(1);
}
set(idx, value) {
const {cur, index} = this.locate(idx);
if (!cur) return;
return cur.chunk[index] = value;
}
inc(idx, value) {
const {cur, index} = this.locate(idx);
if (!cur) return;
return cur.chunk[index] += value;
}
slice(idx, length) {
let {cur, index} = this.locate(idx);
if (!cur) return;
const chunks = [];
if (idx < 0) {
index++;
while (true) {
let len = index - cur.index;
if (len > length) len = length;
length -= len;
const slice = cur.chunk.slice(index - len, index);
chunks.push(slice);
cur = cur.prev;
if (!cur || !length) return chunks.reverse().flat();
index = cur.chunk.length;
}
} else {
while (true) {
let len = cur.chunk.length - index;
if (len > length) len = length;
length -= len;
const slice = cur.chunk.slice(index, index + len);
chunks.push(slice);
cur = cur.next;
if (!cur || !length) return chunks.flat();
index = cur.index;
}
}
}
toArray({reverse} = {}) {

@@ -88,0 +168,0 @@ const chunks = [];

@@ -68,1 +68,26 @@ const Deque = require('./deque');

});
test('Deque: locate, get, set, inc: random access', () => {
const dq = new Deque();
dq.unshift(1);
dq.push(2);
expect(dq.second).toBe(2);
expect(dq.get(-2)).toBe(1);
expect(dq.get(-3)).toBe(undefined);
expect(dq.locate(1)).toEqual({cur: dq._last, index: 0});
expect(dq.locate(2)).toEqual({cur: undefined, index: 0});
expect(dq.locate(-3)).toEqual({cur: undefined, index: -1});
expect(dq.set(1, -1)).toBe(-1);
expect(dq.set(2, -2)).toBe(undefined);
expect(dq.inc(1, -1)).toBe(-2);
expect(dq.inc(2, -3)).toBe(undefined);
});
test('Deque: slice', () => {
const dq = new Deque();
dq.unshift(-2, -1);
dq.push(1, 2);
expect(dq.slice(1, 2)).toEqual([-1, 1]);
expect(dq.slice(-2, 2)).toEqual([-1, 1]);
expect(dq.slice(-5, 2)).toEqual(undefined);
});

@@ -8,11 +8,10 @@ const AsIt = require('./base');

AsIt.value_(async function toRecentGroup(iter, limit = Infinity, opts = {}) {
if (typeof limit === 'object') { opts = limit; limit = Infinity; }
let {to, group, skip, stopOnDropped, stopOnCond} = opts;
if (!to) to = Object.create(null);
const st = {nKeys: 0, nDropped: 0, idx: -1};
AsIt.chain_(async function *recentGroup(iter, to, limit = Infinity, opts = {}) {
if (typeof limit === 'object') { opts = limit; limit = opts.limit || Infinity; }
let {group, skip, stopOnDropped, stopOnCond} = opts;
Object.assign(opts, {nKeys: 0, nDropped: 0, idx: -1});
for await (const item of iter) {
st.idx++;
if (skip && skip--) continue;
opts.idx++;
if (skip && skip--) { yield item; continue; }

@@ -30,31 +29,35 @@ let [k, v] = item instanceof Array ? item : [item, 'count'];

if (st.nKeys >= limit) {
if (++st.nDropped === stopOnDropped) { st.stopped = 'dropped'; break; }
if (opts.nKeys >= limit) {
if (++opts.nDropped === stopOnDropped) { opts.stopped = 'dropped'; break; }
to[k] = ex;
const first = Iter.objectKeys(to).first();
delete to[first];
if (first === k) continue;
if (first === k) { yield item; continue; };
} else {
to[k] = ex;
st.nKeys++;
opts.nKeys++;
}
}
if (stopOnCond && await stopOnCond.call(this, ex, k, v, st)) { st.stopped = 'cond'; break; }
if (stopOnCond && await stopOnCond.call(this, ex, k, v, opts)) { opts.stopped = 'cond'; break; }
$.accumulate(ex, v);
yield item;
}
});
Object.assign(opts, st);
return to;
AsIt.value_(async function toRecentGroup(iter, limit = Infinity, opts = {}) {
if (typeof limit === 'object') { opts = limit; limit = opts.limit || Infinity; }
if (!opts.to) opts.to = Object.create(null);
for await (const item of AsIt.recentGroup.gen(iter, opts.to, limit, opts));
return opts.to;
});
AsIt.value_(async function toOrderGroup(iter, limit = Infinity, opts = {}) {
if (typeof limit === 'object') { opts = limit; limit = Infinity; }
let {to, group, skip, stopOnDropped, stopOnCond} = opts;
if (!to) to = Object.create(null);
const st = {nKeys: 0, nDropped: 0, idx: -1};
AsIt.chain_(async function *orderGroup(iter, to, limit = Infinity, opts = {}) {
if (typeof limit === 'object') { opts = limit; limit = opts.limit || Infinity; }
let {group, skip, stopOnDropped, stopOnCond} = opts;
Object.assign(opts, {nKeys: 0, nDropped: 0, idx: -1});
for await (const item of iter) {
st.idx++;
if (skip && skip--) continue;
opts.idx++;
if (skip && skip--) { yield item; continue; }

@@ -69,22 +72,27 @@ let [k, v] = item instanceof Array ? item : [item, 'count'];

if (st.nKeys >= limit) {
if (++st.nDropped === stopOnDropped) { st.stopped = 'dropped'; break; }
if (opts.nKeys >= limit) {
if (++opts.nDropped === stopOnDropped) { opts.stopped = 'dropped'; break; }
to[k] = ex;
const first = Iter.objectKeys(to).first();
delete to[first];
if (first === k) continue;
if (first === k) { yield item; continue; };
} else {
to[k] = ex;
st.nKeys++;
opts.nKeys++;
}
}
if (stopOnCond && await stopOnCond.call(this, ex, k, v, st)) { st.stopped = 'cond'; break; }
if (stopOnCond && await stopOnCond.call(this, ex, k, v, opts)) { opts.stopped = 'cond'; break; }
$.accumulate(ex, v);
yield item;
}
});
Object.assign(opts, st);
return to;
AsIt.value_(async function toOrderGroup(iter, limit = Infinity, opts = {}) {
if (typeof limit === 'object') { opts = limit; limit = opts.limit || Infinity; }
if (!opts.to) opts.to = Object.create(null);
for await (const item of AsIt.orderGroup.gen(iter, opts.to, limit, opts));
return opts.to;
});
module.exports = AsIt;
const AsIt = require('./aggr');
async function asItArray(iter) {
const res = [];
for await (const item of iter) res.push(item);
return res;
}
test('AsIt_.toRecentGroup: default', async () => {

@@ -16,4 +22,5 @@ const src = new AsIt(AsIt.getIter(['a', 'b', 'c', 'a', ['d', 'custom']]));

const opts = {stopOnDropped: 1};
expect(await src.toRecentGroup(3, opts)).toEqual({a: {count: 2}, b: {count: 1}, c: {count: 1}});
expect(opts).toEqual({stopOnDropped: 1, stopped: 'dropped', nKeys: 3, nDropped: 1, idx: 4});
const to = await src.toRecentGroup(3, opts);
expect(to).toEqual({a: {count: 2}, b: {count: 1}, c: {count: 1}});
expect(opts).toEqual({stopOnDropped: 1, stopped: 'dropped', nKeys: 3, nDropped: 1, idx: 4, to});
});

@@ -63,4 +70,5 @@

const opts = {stopOnDropped: 1};
expect(await src.toOrderGroup(3, opts)).toEqual({a: {count: 2}, b: {count: 1}, c: {count: 1}});
expect(opts).toEqual({stopOnDropped: 1, stopped: 'dropped', nKeys: 3, nDropped: 1, idx: 4});
const to = await src.toOrderGroup(3, opts);
expect(to).toEqual({a: {count: 2}, b: {count: 1}, c: {count: 1}});
expect(opts).toEqual({stopOnDropped: 1, stopped: 'dropped', nKeys: 3, nDropped: 1, idx: 4, to});
});

@@ -74,1 +82,14 @@

});
test('AsIt_.recentGroup, orderGroup: nosort, sort', async () => {
const from = ['a', 'b', 'c', 'a', ['d', 'custom']];
const src = new AsIt(AsIt.getIter(from));
const recent = {};
const order = {};
const group = await asItArray(src.recentGroup(recent, {}).orderGroup(order, {}));
expect(group).toEqual(from);
expect(order).toEqual({a: {count: 2}, b: {count: 1}, c: {count: 1}, d: {custom: 1}});
expect(Object.keys(order)).toEqual(['a', 'b', 'c', 'd']);
expect(recent).toEqual({a: {count: 2}, b: {count: 1}, c: {count: 1}, d: {custom: 1}});
expect(Object.keys(recent)).toEqual(['b', 'c', 'a', 'd']);
});

@@ -39,2 +39,16 @@ const {Readable} = require('stream');

AsIt.chain_(async function *unset(iter, to) {
for await (const item of iter) {
to.delete(item);
yield item;
}
});
AsIt.chain_(async function *omit(iter, to) {
for await (const item of iter) {
delete to[item];
yield item;
}
});
AsIt.value_(async function toSet(iter, to) {

@@ -41,0 +55,0 @@ if (!to) to = new Set();

@@ -67,2 +67,10 @@ const cr = require('crypto');

test('AsIt_.unset: unset from Set or Map', async () => {
const wrapped = new AsIt([2, 6, 7, 6][Symbol.iterator]());
const to = new Set([1, 2, 6, 10]);
wrapped.unset(to);
expect(await asItArray(wrapped)).toEqual([2, 6, 7, 6]);
expect(Array.from(to)).toEqual([1, 10]);
});
test('AsIt_.toSet: grab to set', async () => {

@@ -109,2 +117,10 @@ const wrapped = new AsIt([2, 6, 7][Symbol.iterator]());

test('AsIt_.omit: omit from object', async () => {
const wrapped = new AsIt(['a', 'b', 'c'][Symbol.iterator]());
const to = {a: 5, d: 8, x: 1, c: 2};
wrapped.omit(to);
expect(await asItArray(wrapped)).toEqual(['a', 'b', 'c']);
expect(to).toEqual({d: 8, x: 1});
});
test('AsIt_.toObject: get object from entries', async () => {

@@ -111,0 +127,0 @@ const entries = new AsIt([['a', 1], ['b', 2], 'c', null][Symbol.iterator]());

@@ -218,2 +218,14 @@ const $ = require('../base');

func_(function window_(n, buf) {
if (!Number.isInteger(n) || n <= 0) n = 2;
if (!buf) buf = new $.DQ();
return function _window(v) {
if (buf.push(v) > n) buf.shift();
return buf;
};
});
func_($.window_(), 'window');
func_(function save_(id) {

@@ -241,2 +253,32 @@ return function _save(value) {

func_(function relay_(...funcs) {
const desc = {ctx: this};
$._mappingFuncs(funcs);
return function _relay(value) {
let v = value;
for (const func of funcs) {
v = func.call(this, v, value, desc);
}
return v;
};
});
func_(function arelay_(...funcs) {
const desc = {ctx: this};
$._mappingFuncs(funcs);
return async function _relay(value) {
let v = value;
for (const func of funcs) {
v = await func.call(this, v, value, desc);
}
return v;
};
});
func_(function _mappingFuncs(funcs, iterOut) {

@@ -243,0 +285,0 @@ const l = funcs.length;

@@ -175,1 +175,13 @@ const $ = require('./map');

});
test('$.relay_: sync func relay group', () => {
const relay = $.relay_(v => v + 1, v => v * 2);
expect(relay(4)).toBe(10);
});
test('$.arelay_: async func relay group', async () => {
const relay = $.arelay_(async v => v + 1, v => v * 2);
const relayed = relay(4);
expect(relayed instanceof Promise).toBe(true);
expect(await relayed).toBe(10);
});

@@ -1,2 +0,2 @@

const $ = require('../base');
const $ = require('./map');

@@ -19,3 +19,3 @@ const {func_} = $;

func_(function sort_(key) {
func_(function sortField_(key) {
if (key == null) key = 0;

@@ -32,4 +32,20 @@

func_($.sort_(), 'sortKey');
func_($.sortField_(), 'sortKey');
const refTypes = new Set(['object', 'function']);
func_(function sort_(...funcs) {
const func = funcs[0];
if (funcs.length === 1 && (typeof func === 'string' || typeof func === 'symbol')) return $.sortField_(func);
const sort = $.relay_.apply(this, funcs);
return function _sort(A, B) {
const a = sort(A);
const b = sort(B);
if (a > b) return 1;
if (a < b) return -1;
return 0;
};
});
func_(function lastElem(arr, back = 0) {

@@ -36,0 +52,0 @@ if (arr instanceof Array) return arr[arr.length - back - 1];

@@ -33,2 +33,21 @@ const $ = require('./sort');

test('$.sort: sort by entry key', () => {
const arr = [[6, 9], [1, 8, 'a'], [9.9, {a: 1}], [22, 'x'], [1], [0]];
const sorted = arr.sort($.sort_('0'));
expect(sorted).toEqual([[0], [1, 8, 'a'], [1], [6, 9], [9.9, {a: 1}], [22, 'x']]);
});
test('$.sort: sort by symbol', () => {
const S = Symbol('S');
const arr = [{[S]: 2}, {[S]: 1}];
const sorted = arr.sort($.sort_(S));
expect(sorted).toEqual([{[S]: 1}, {[S]: 2}]);
});
test('$.sort_: sort by functional', () => {
const arr = [[{a: 6}, 9], [{a: 1}, 8, 'a'], [{a: 9.9}, {a: 1}], [{a: 22}, 'x'], [{a: 1}], [{x: 0, a: 0}]];
const sorted = arr.sort($.sort_('0', 'a'));
expect(sorted).toEqual([[{x: 0, a: 0}], [{a: 1}, 8, 'a'], [{a: 1}], [{a: 6}, 9], [{a: 9.9}, {a: 1}], [{a: 22}, 'x']]);
});
test('$.lastElem: get last element / element back from end', () => {

@@ -35,0 +54,0 @@ expect($.lastElem({})).toBe(null);

@@ -416,2 +416,5 @@ const $ = require('../func');

pingInterval: null, //TODO:
fatalErrors: [], //TODO: $_method_ -- idempotent method (retry on fatal error)
sleepImmediate: false,

@@ -449,2 +452,19 @@ sleepIdle: 120000,

const IoCs = new WeakMap();
func_(function IoC(config, ...args) {
let ofClass = IoCs.get(this);
if (!ofClass) {
ofClass = new WeakMap();
IoCs.set(this, ofClass);
}
const exist = ofClass.get(config);
if (exist) return exist;
const inst = new this(config, ...args);
ofClass.set(config, inst);
return inst;
});
module.exports = $;

@@ -498,1 +498,10 @@ const $ = require('./class');

});
test('class: IoC', () => {
const config1 = $();
const config2 = $();
const inst1 = MyEmpty.IoC(config1);
const inst2 = MyEmpty.IoC(config2);
expect(MyEmpty.IoC(config1)).toBe(inst1);
expect(MyEmpty.IoC(config2)).toBe(inst2);
});

@@ -6,3 +6,5 @@ const $ = require('../func');

require('./class');
require('./cache');
require('./graph');
module.exports = $;

@@ -9,11 +9,10 @@ const Iter = require('./base');

Iter.value_(function toRecentGroup(iter, limit = Infinity, opts = {}) {
if (typeof limit === 'object') { opts = limit; limit = Infinity; }
let {to, group, skip, stopOnDropped, stopOnCond} = opts;
if (!to) to = Object.create(null);
const st = {nKeys: 0, nDropped: 0, idx: -1};
Iter.chain_(function *recentGroup(iter, to, limit = Infinity, opts = {}) {
if (typeof limit === 'object') { opts = limit; limit = opts.limit || Infinity; }
let {group, skip, stopOnDropped, stopOnCond} = opts;
Object.assign(opts, {nKeys: 0, nDropped: 0, idx: -1});
for (const item of iter) {
st.idx++;
if (skip && skip--) continue;
opts.idx++;
if (skip && skip--) { yield item; continue; };

@@ -31,31 +30,35 @@ let [k, v] = item instanceof Array ? item : [item, 'count'];

if (st.nKeys >= limit) {
if (++st.nDropped === stopOnDropped) { st.stopped = 'dropped'; break; }
if (opts.nKeys >= limit) {
if (++opts.nDropped === stopOnDropped) { opts.stopped = 'dropped'; break; }
to[k] = ex;
const first = Iter.objectKeys(to).first();
delete to[first];
if (first === k) continue;
if (first === k) { yield item; continue; };
} else {
to[k] = ex;
st.nKeys++;
opts.nKeys++;
}
}
if (stopOnCond && stopOnCond.call(this, ex, k, v, st)) { st.stopped = 'cond'; break; }
if (stopOnCond && stopOnCond.call(this, ex, k, v, opts)) { opts.stopped = 'cond'; break; }
$.accumulate(ex, v);
yield item;
}
});
Object.assign(opts, st);
return to;
Iter.value_(function toRecentGroup(iter, limit = Infinity, opts = {}) {
if (typeof limit === 'object') { opts = limit; limit = opts.limit || Infinity; }
if (!opts.to) opts.to = Object.create(null);
for (const item of Iter.recentGroup.gen(iter, opts.to, limit, opts));
return opts.to;
});
Iter.value_(function toOrderGroup(iter, limit = Infinity, opts = {}) {
if (typeof limit === 'object') { opts = limit; limit = Infinity; }
let {to, group, skip, stopOnDropped, stopOnCond} = opts;
if (!to) to = Object.create(null);
const st = {nKeys: 0, nDropped: 0, idx: -1};
Iter.chain_(function *orderGroup(iter, to, limit = Infinity, opts = {}) {
if (typeof limit === 'object') { opts = limit; limit = opts.limit || Infinity; }
let {group, skip, stopOnDropped, stopOnCond} = opts;
Object.assign(opts, {nKeys: 0, nDropped: 0, idx: -1});
for (const item of iter) {
st.idx++;
if (skip && skip--) continue;
opts.idx++;
if (skip && skip--) { yield item; continue; };

@@ -70,22 +73,30 @@ let [k, v] = item instanceof Array ? item : [item, 'count'];

if (st.nKeys >= limit) {
if (++st.nDropped === stopOnDropped) { st.stopped = 'dropped'; break; }
if (opts.nKeys >= limit) {
if (++opts.nDropped === stopOnDropped) { opts.stopped = 'dropped'; break; }
to[k] = ex;
const first = Iter.objectKeys(to).first();
delete to[first];
if (first === k) continue;
if (first === k) { yield item; continue; };
} else {
to[k] = ex;
st.nKeys++;
opts.nKeys++;
}
}
if (stopOnCond && stopOnCond.call(this, ex, k, v, st)) { st.stopped = 'cond'; break; }
if (stopOnCond && stopOnCond.call(this, ex, k, v, opts)) { opts.stopped = 'cond'; break; }
$.accumulate(ex, v);
yield item;
}
Object.assign(opts, st);
Object.assign(opts, opts);
return to;
});
Iter.value_(function toOrderGroup(iter, limit = Infinity, opts = {}) {
if (typeof limit === 'object') { opts = limit; limit = opts.limit || Infinity; }
if (!opts.to) opts.to = Object.create(null);
for (const item of Iter.orderGroup.gen(iter, opts.to, limit, opts));
return opts.to;
});
module.exports = Iter;

@@ -16,4 +16,5 @@ const Iter = require('./aggr');

const opts = {stopOnDropped: 1};
expect(src.toRecentGroup(3, opts)).toEqual({a: {count: 2}, b: {count: 1}, c: {count: 1}});
expect(opts).toEqual({stopOnDropped: 1, stopped: 'dropped', nKeys: 3, nDropped: 1, idx: 4});
const to = {a: {count: 2}, b: {count: 1}, c: {count: 1}};
expect(src.toRecentGroup(3, opts)).toEqual(to);
expect(opts).toEqual({stopOnDropped: 1, stopped: 'dropped', nKeys: 3, nDropped: 1, idx: 4, to});
});

@@ -63,4 +64,5 @@

const opts = {stopOnDropped: 1};
expect(src.toOrderGroup(3, opts)).toEqual({a: {count: 2}, b: {count: 1}, c: {count: 1}});
expect(opts).toEqual({stopOnDropped: 1, stopped: 'dropped', nKeys: 3, nDropped: 1, idx: 4});
const to = {a: {count: 2}, b: {count: 1}, c: {count: 1}};
expect(src.toOrderGroup(3, opts)).toEqual(to);
expect(opts).toEqual({stopOnDropped: 1, stopped: 'dropped', nKeys: 3, nDropped: 1, idx: 4, to});
});

@@ -74,1 +76,14 @@

});
test('Iter_.recentGroup, orderGroup: nosort, sort', () => {
const from = ['a', 'b', 'c', 'a', ['d', 'custom']];
const src = new Iter(Iter.getIter(from));
const recent = {};
const order = {};
const group = Array.from(src.recentGroup(recent, {}).orderGroup(order, {}));
expect(group).toEqual(from);
expect(order).toEqual({a: {count: 2}, b: {count: 1}, c: {count: 1}, d: {custom: 1}});
expect(Object.keys(order)).toEqual(['a', 'b', 'c', 'd']);
expect(recent).toEqual({a: {count: 2}, b: {count: 1}, c: {count: 1}, d: {custom: 1}});
expect(Object.keys(recent)).toEqual(['b', 'c', 'a', 'd']);
});

@@ -41,2 +41,16 @@ const {Readable} = require('stream');

Iter.chain_(function *unset(iter, to) {
for (const item of iter) {
to.delete(item);
yield item;
}
});
Iter.chain_(function *omit(iter, to) {
for (const item of iter) {
delete to[item];
yield item;
}
});
Iter.value_(function toSet(iter, to) {

@@ -43,0 +57,0 @@ if (to) {

@@ -60,2 +60,10 @@ const cr = require('crypto');

test('Iter_.unset: unset from Set or Map', () => {
const wrapped = new Iter([2, 6, 7, 6][Symbol.iterator]());
const to = new Set([1, 2, 6, 10]);
wrapped.unset(to);
expect(Array.from(wrapped)).toEqual([2, 6, 7, 6]);
expect(Array.from(to)).toEqual([1, 10]);
});
test('Iter_.toSet: grab to set', () => {

@@ -102,2 +110,10 @@ const wrapped = new Iter([2, 6, 7, 6][Symbol.iterator]());

test('Iter_.omit: omit from object', () => {
const wrapped = new Iter(['a', 'b', 'c'][Symbol.iterator]());
const to = {a: 5, d: 8, x: 1, c: 2};
wrapped.omit(to);
expect(Array.from(wrapped)).toEqual(['a', 'b', 'c']);
expect(to).toEqual({d: 8, x: 1});
});
test('Iter_.toObject: get object from entries', () => {

@@ -104,0 +120,0 @@ const entries = new Iter([['a', 1], ['b', 0], 'c', ['b', 2], null][Symbol.iterator]());

{
"name": "asclasit",
"version": "0.1.3",
"version": "0.1.4",
"description": "ASync CLasses + ASync ITerators",

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc