Skip to content

Commit

Permalink
add src
Browse files Browse the repository at this point in the history
  • Loading branch information
litstat committed Mar 1, 2024
1 parent 59f6cdd commit 8e6e0b1
Show file tree
Hide file tree
Showing 42 changed files with 1,555 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/append.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { nativeIsArray } from 'underscore/modules/_setup.js';
import { array } from './array';

/**
* Append arguments to shallow array
*/
export function append(a) {
a = array(a);

for (var i = 1; i < arguments.length; i++) {
var item = arguments[i];

if (nativeIsArray(item)) {
append(a, ...item);
}
else {
a.push(item);
}
}

return a;
}
74 changes: 74 additions & 0 deletions src/apply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import has from 'underscore/modules/_has.js';
import { nativeIsArray } from 'underscore/modules/_setup.js';
import isUndefined from 'underscore/modules/isUndefined.js';

/**
* Copy own properties from objects 'overrides' to object 'o'.
*
* @param {object} o destination object
* @param {array} [del] array of properties to delete from 'o'
* @param {...object} overrides source objects
* @return {object} changed object 'o'
*/
export function applyOwn(o, del, ...overrides) {
clean(o, del, overrides);

return Object.assign(o, ...overrides);
}

/**
* Copy own properties from 'defaults' to 'o' if not exists
*/
export function applyOwnIf(o, del, ...defaults) {
clean(o, del, defaults);

for (var source, i = 0; i < defaults.length; i++) {
for (var k in source = defaults[i]) {
if (!has(source, k)) {
break;
}

if (isUndefined(o[k])) {
o[k] = source[k];
}
}
}

return o;
}

function clean(o, del, rest) {
if (nativeIsArray(del)) {
del.forEach(k => {
delete o[k];
});
}
else if (del) {
rest.unshift(del);
}
}

const splitRe = /[,;\s]+/;

/**
* Copy properties from source object to destination object.
*/
export function applyTo(dst, src, ...keys) {
if (nativeIsArray(keys[0])) {
keys = keys[0];
}

if (keys.length === 1) {
keys = keys[0].split(splitRe);
}

for (var i = 0; i < keys.length; i++) {
var k = keys[i];

if (!isUndefined(src[k])) {
dst[k] = src[k];
}
}

return dst;
}
23 changes: 23 additions & 0 deletions src/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { nativeIsArray } from 'underscore/modules/_setup.js';
import { slice } from 'underscore/modules/_setup.js';
import isArguments from 'underscore/modules/isArguments.js';
import isUndefined from 'underscore/modules/isUndefined.js';

/**
* Convert value to array if it's not array
*/
export function array(a = []) {
if (nativeIsArray(a)) {
return a;
}

if (isUndefined(a)) {
return [];
}

if (isArguments(a)) {
return slice.call(a);
}

return [ a ];
}
43 changes: 43 additions & 0 deletions src/arrayBy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import isFunction from 'underscore/modules/isFunction.js';

import { inc } from './inc';
import { push } from './push';
import { transform } from './transform';

/**
* Count values by key
*/
export function countBy(a, k) {
return iteratee(a, k, one, inc);
}

function one() {
return 1;
}

/**
* Group array values by key
*/
export function groupBy(a, k, v) {
return iteratee(a, k, v, push);
}

/**
* Array mapper
*/
export function indexBy(a, k, v) {
return iteratee(a, k, v, index);
}

function index(o, k, v) {
o[k] = v;
}

function iteratee(a, k, v, fn) {
var kFn = isFunction(k) ? k : (item) => k ? item[k] : item;
var vFn = isFunction(v) ? v : (item) => v ? item[v] : item;

return transform(a, (o, item) => {
fn(o, kFn(item), vFn(item));
}, {});
}
52 changes: 52 additions & 0 deletions src/buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { log } from './log';

const buffers = {};

/**
* Wait 'delay' after stop continuous call 'buffer' and run 'fn'.
*
* ex.:
* continuous call
* buffer('update', item1)
* buffer('update', item2)
* buffer('update', item3)
* ...
* will cause after delay
* scope.fn([ item1, item2, item3, ... ])
*/
export function buffer(name, item, fn, scope, delay) {
return (
buffers[name] ||
(buffers[name] = creator(name, fn, scope, delay))
)(item);
}

function creator(name, fn, scope, delay) {
var items = {};
var last = 0;
var size = 0;

var timer = setInterval(function() {
if (last !== size) {
last = size;
return;
}

clearInterval(timer);

// detach queue
delete buffers[name];

items = Object.values(items);

log('process buffer', { name, size, items, fn });

// return queue items
fn.call(scope, items);
}, delay || 50);

return function(item) {
items[item && item.id || item] = item;
++size;
};
}
55 changes: 55 additions & 0 deletions src/canary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { applyOwn } from './apply';
import { log } from './log';

const o = {
cookie: 'canary',
enabled: 'always',
disabled: 'never',
random,
};

var value;

/**
* Set canary cookie for days amount.
* Get current value if was called without parameters.
*/
export function canary(enable, days = 7) {
var was = o.enabled === value;

if (arguments.length === 0) {
return was;
}

value = enable ? o.enabled : o.disabled;
log(o.cookie, 'set to', '"' + value + '"', 'for', days, 'days');
document.cookie = o.cookie + '=' + value + ';max-age=' + 60 * 60 * 24 * days;

if (!!enable ^ was) {
location.reload();
}
}

applyOwn(canary, {
init,
});

function init(config) {
applyOwn(o, config);

document.cookie.split('; ').some(pair =>
pair.slice(0, 7) === o.cookie + '=' &&
(value = pair.slice(7))
);

if (value) {
log(o.cookie, '=', '"' + value + '"');
}
else {
canary(o.random());
}
}

function random() {
return Math.random() < 0.1;
}
7 changes: 7 additions & 0 deletions src/capitalize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function capitalize(s) {
if (s) {
s = s[0].toUpperCase() + s.slice(1);
}

return s;
}
Loading

0 comments on commit 8e6e0b1

Please sign in to comment.