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

@fastkit/helpers

Package Overview
Dependencies
Maintainers
1
Versions
204
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastkit/helpers - npm Package Compare versions

Comparing version 0.2.13 to 0.3.0

187

dist/helpers.cjs.js

@@ -14,2 +14,20 @@ 'use strict';

}
function toComparableNumbers(...args) {
let maxFloating = 0;
args.forEach((arg) => {
const floating = arg.toString().split('.')[1];
if (floating) {
const { length } = floating;
if (length > maxFloating) {
maxFloating = length;
}
}
});
const offset = 10 ** maxFloating;
return [args.map((arg) => arg * offset), offset];
}
function safeRemainderOperation(a, b) {
const [[_a, _b]] = toComparableNumbers(a, b);
return _a % _b;
}

@@ -22,2 +40,18 @@ function isPromise(obj) {

/** Checks if value is string */
function isString(source) {
return typeof source === 'string' || source instanceof String;
}
function nilToEmptyString(source) {
return source == null ? '' : source;
}
function toHalfWidth(source) {
return nilToEmptyString(source).replace(/[!-~]/g, (source) => {
return String.fromCharCode(source.charCodeAt(0) - 0xfee0);
});
}
function toSingleSpace(source) {
return nilToEmptyString(source).replace(/([\s]+)/g, (m) => m.charAt(0));
}
const HAS_SET = typeof Set !== 'undefined';

@@ -33,2 +67,51 @@ function isSet(source) {

/** Escapes regular expression control chars */
function escapeRegExp(str) {
return str.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
}
// cloned from https://github.com/epoberezkin/fast-deep-equal with small changes
function objectIncludes(b, a) {
if (a === b)
return true;
const arrA = Array.isArray(a);
const arrB = Array.isArray(b);
let i;
if (arrA && arrB) {
if (a.length != b.length)
return false;
for (i = 0; i < a.length; i++)
if (!objectIncludes(a[i], b[i]))
return false;
return true;
}
if (arrA != arrB)
return false;
if (a && b && typeof a === 'object' && typeof b === 'object') {
const dateA = a instanceof Date, dateB = b instanceof Date;
if (dateA && dateB)
return a.getTime() == b.getTime();
if (dateA != dateB)
return false;
const regexpA = a instanceof RegExp, regexpB = b instanceof RegExp;
if (regexpA && regexpB)
return a.toString() == b.toString();
if (regexpA != regexpB)
return false;
const keys = Object.keys(a);
// if (keys.length !== Object.keys(b).length) return false;
for (i = 0; i < keys.length; i++)
if (!Object.prototype.hasOwnProperty.call(b, keys[i]))
return false;
for (i = 0; i < keys.length; i++)
if (!objectIncludes(b[keys[i]], a[keys[i]]))
return false;
return true;
}
else if (a && b && typeof a === 'function' && typeof b === 'function') {
return a.toString() === b.toString();
}
return false;
}
function flattenRecursiveArray(source) {

@@ -68,2 +151,57 @@ const results = [];

function safeGetTimeByDateSource(source) {
if (typeof source === 'number')
return source;
if (typeof source === 'string') {
source = new Date(source);
}
return source.getTime();
}
const DATE_INPUT_PRECISIONS = [
'month',
'date',
'datetime-local',
];
const DATE_INPUT_RE = /^(\d{4})-(\d{2})(-(\d{2})(T(\d{2}):(\d{2}))?)?$/;
function parseDateInput(source) {
let result = null;
const matched = source.match(DATE_INPUT_RE);
if (matched) {
const year = matched[1];
const month = matched[2];
const date = matched[4];
const hours = matched[6];
const minutes = matched[7];
if (hours && minutes) {
result = {
precision: 'datetime-local',
year,
month,
date,
hours,
minutes,
source,
};
}
else if (date) {
result = {
precision: 'date',
year,
month,
date,
source,
};
}
else if (year && month) {
result = {
precision: 'month',
year,
month,
source,
};
}
}
return result;
}
function Cloner(opts = {}) {

@@ -288,2 +426,9 @@ const { keyProcesser, valueProcesser } = opts;

}
function ownerDocument(node) {
return (node && node.ownerDocument) || document;
}
function ownerWindow(node) {
const doc = ownerDocument(node);
return doc.defaultView || window;
}

@@ -320,3 +465,3 @@ function resolveDebounceSettings(fnOrSettings, delayOrOptions, immediateOption) {

const { handler, immediate } = resolved;
let { delay = 100 } = resolved;
let { delay = 166 } = resolved;
let booted = false;

@@ -378,3 +523,29 @@ let timerId;

/**
* 値が空っぽでないか
*
* 配列の場合はlengthをチェックする
* falseは値として認めない(チェックボックスを考慮)
* value === null
* || value === undefined
* || value === ''
* || Array.isArray(value) && value.length === 0
*/
function isEmpty(value) {
if (typeof value === 'function')
return false;
return value == null || value === false || value.length === 0;
}
function notEmptyValue(args, defaultValue) {
for (const arg of args) {
if (!isEmpty(arg)) {
return arg;
}
}
return defaultValue;
}
exports.Cloner = Cloner;
exports.DATE_INPUT_PRECISIONS = DATE_INPUT_PRECISIONS;
exports.DATE_INPUT_RE = DATE_INPUT_RE;
exports.IN_DOCUMENT = IN_DOCUMENT;

@@ -390,2 +561,3 @@ exports.IN_WINDOW = IN_WINDOW;

exports.debounce = debounce;
exports.escapeRegExp = escapeRegExp;
exports.flattenRecursiveArray = flattenRecursiveArray;

@@ -395,2 +567,3 @@ exports.focusFirstDescendant = focusFirstDescendant;

exports.isBuffer = isBuffer;
exports.isEmpty = isEmpty;
exports.isFocusable = isFocusable;

@@ -400,8 +573,20 @@ exports.isMap = isMap;

exports.isSet = isSet;
exports.isString = isString;
exports.nilToEmptyString = nilToEmptyString;
exports.notEmptyValue = notEmptyValue;
exports.objectIncludes = objectIncludes;
exports.ownerDocument = ownerDocument;
exports.ownerWindow = ownerWindow;
exports.parseDateInput = parseDateInput;
exports.pushDynamicStyle = pushDynamicStyle;
exports.resolveDebounceSettings = resolveDebounceSettings;
exports.safeGetTimeByDateSource = safeGetTimeByDateSource;
exports.safeJSONSerializer = safeJSONSerializer;
exports.safeJSONStringify = safeJSONStringify;
exports.safeRemainderOperation = safeRemainderOperation;
exports.toComparableNumbers = toComparableNumbers;
exports.toFloat = toFloat;
exports.toHalfWidth = toHalfWidth;
exports.toInt = toInt;
exports.toNumber = toNumber;
exports.toSingleSpace = toSingleSpace;

@@ -14,2 +14,20 @@ 'use strict';

}
function toComparableNumbers(...args) {
let maxFloating = 0;
args.forEach((arg) => {
const floating = arg.toString().split('.')[1];
if (floating) {
const { length } = floating;
if (length > maxFloating) {
maxFloating = length;
}
}
});
const offset = 10 ** maxFloating;
return [args.map((arg) => arg * offset), offset];
}
function safeRemainderOperation(a, b) {
const [[_a, _b]] = toComparableNumbers(a, b);
return _a % _b;
}

@@ -22,2 +40,18 @@ function isPromise(obj) {

/** Checks if value is string */
function isString(source) {
return typeof source === 'string' || source instanceof String;
}
function nilToEmptyString(source) {
return source == null ? '' : source;
}
function toHalfWidth(source) {
return nilToEmptyString(source).replace(/[!-~]/g, (source) => {
return String.fromCharCode(source.charCodeAt(0) - 0xfee0);
});
}
function toSingleSpace(source) {
return nilToEmptyString(source).replace(/([\s]+)/g, (m) => m.charAt(0));
}
const HAS_SET = typeof Set !== 'undefined';

@@ -33,2 +67,51 @@ function isSet(source) {

/** Escapes regular expression control chars */
function escapeRegExp(str) {
return str.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
}
// cloned from https://github.com/epoberezkin/fast-deep-equal with small changes
function objectIncludes(b, a) {
if (a === b)
return true;
const arrA = Array.isArray(a);
const arrB = Array.isArray(b);
let i;
if (arrA && arrB) {
if (a.length != b.length)
return false;
for (i = 0; i < a.length; i++)
if (!objectIncludes(a[i], b[i]))
return false;
return true;
}
if (arrA != arrB)
return false;
if (a && b && typeof a === 'object' && typeof b === 'object') {
const dateA = a instanceof Date, dateB = b instanceof Date;
if (dateA && dateB)
return a.getTime() == b.getTime();
if (dateA != dateB)
return false;
const regexpA = a instanceof RegExp, regexpB = b instanceof RegExp;
if (regexpA && regexpB)
return a.toString() == b.toString();
if (regexpA != regexpB)
return false;
const keys = Object.keys(a);
// if (keys.length !== Object.keys(b).length) return false;
for (i = 0; i < keys.length; i++)
if (!Object.prototype.hasOwnProperty.call(b, keys[i]))
return false;
for (i = 0; i < keys.length; i++)
if (!objectIncludes(b[keys[i]], a[keys[i]]))
return false;
return true;
}
else if (a && b && typeof a === 'function' && typeof b === 'function') {
return a.toString() === b.toString();
}
return false;
}
function flattenRecursiveArray(source) {

@@ -68,2 +151,57 @@ const results = [];

function safeGetTimeByDateSource(source) {
if (typeof source === 'number')
return source;
if (typeof source === 'string') {
source = new Date(source);
}
return source.getTime();
}
const DATE_INPUT_PRECISIONS = [
'month',
'date',
'datetime-local',
];
const DATE_INPUT_RE = /^(\d{4})-(\d{2})(-(\d{2})(T(\d{2}):(\d{2}))?)?$/;
function parseDateInput(source) {
let result = null;
const matched = source.match(DATE_INPUT_RE);
if (matched) {
const year = matched[1];
const month = matched[2];
const date = matched[4];
const hours = matched[6];
const minutes = matched[7];
if (hours && minutes) {
result = {
precision: 'datetime-local',
year,
month,
date,
hours,
minutes,
source,
};
}
else if (date) {
result = {
precision: 'date',
year,
month,
date,
source,
};
}
else if (year && month) {
result = {
precision: 'month',
year,
month,
source,
};
}
}
return result;
}
function Cloner(opts = {}) {

@@ -288,2 +426,9 @@ const { keyProcesser, valueProcesser } = opts;

}
function ownerDocument(node) {
return (node && node.ownerDocument) || document;
}
function ownerWindow(node) {
const doc = ownerDocument(node);
return doc.defaultView || window;
}

@@ -320,3 +465,3 @@ function resolveDebounceSettings(fnOrSettings, delayOrOptions, immediateOption) {

const { handler, immediate } = resolved;
let { delay = 100 } = resolved;
let { delay = 166 } = resolved;
let booted = false;

@@ -378,3 +523,29 @@ let timerId;

/**
* 値が空っぽでないか
*
* 配列の場合はlengthをチェックする
* falseは値として認めない(チェックボックスを考慮)
* value === null
* || value === undefined
* || value === ''
* || Array.isArray(value) && value.length === 0
*/
function isEmpty(value) {
if (typeof value === 'function')
return false;
return value == null || value === false || value.length === 0;
}
function notEmptyValue(args, defaultValue) {
for (const arg of args) {
if (!isEmpty(arg)) {
return arg;
}
}
return defaultValue;
}
exports.Cloner = Cloner;
exports.DATE_INPUT_PRECISIONS = DATE_INPUT_PRECISIONS;
exports.DATE_INPUT_RE = DATE_INPUT_RE;
exports.IN_DOCUMENT = IN_DOCUMENT;

@@ -390,2 +561,3 @@ exports.IN_WINDOW = IN_WINDOW;

exports.debounce = debounce;
exports.escapeRegExp = escapeRegExp;
exports.flattenRecursiveArray = flattenRecursiveArray;

@@ -395,2 +567,3 @@ exports.focusFirstDescendant = focusFirstDescendant;

exports.isBuffer = isBuffer;
exports.isEmpty = isEmpty;
exports.isFocusable = isFocusable;

@@ -400,8 +573,20 @@ exports.isMap = isMap;

exports.isSet = isSet;
exports.isString = isString;
exports.nilToEmptyString = nilToEmptyString;
exports.notEmptyValue = notEmptyValue;
exports.objectIncludes = objectIncludes;
exports.ownerDocument = ownerDocument;
exports.ownerWindow = ownerWindow;
exports.parseDateInput = parseDateInput;
exports.pushDynamicStyle = pushDynamicStyle;
exports.resolveDebounceSettings = resolveDebounceSettings;
exports.safeGetTimeByDateSource = safeGetTimeByDateSource;
exports.safeJSONSerializer = safeJSONSerializer;
exports.safeJSONStringify = safeJSONStringify;
exports.safeRemainderOperation = safeRemainderOperation;
exports.toComparableNumbers = toComparableNumbers;
exports.toFloat = toFloat;
exports.toHalfWidth = toHalfWidth;
exports.toInt = toInt;
exports.toNumber = toNumber;
exports.toSingleSpace = toSingleSpace;

@@ -45,2 +45,8 @@ /// <reference types="node" />

export declare const DATE_INPUT_PRECISIONS: readonly ["month", "date", "datetime-local"];
export declare const DATE_INPUT_RE: RegExp;
export declare type DateInputPrecision = typeof DATE_INPUT_PRECISIONS[number];
export declare type Debouncable = (...args: any[]) => any;

@@ -70,2 +76,5 @@

/** Escapes regular expression control chars */
export declare function escapeRegExp(str: string): string;
export declare function flattenRecursiveArray<T>(source: RecursiveArray<T>): T[];

@@ -83,2 +92,14 @@

/**
* 値が空っぽでないか
*
* 配列の場合はlengthをチェックする
* falseは値として認めない(チェックボックスを考慮)
* value === null
* || value === undefined
* || value === ''
* || Array.isArray(value) && value.length === 0
*/
export declare function isEmpty(value: any): boolean;
export declare function isFocusable(element: HTMLElement): boolean;

@@ -92,2 +113,17 @@

/** Checks if value is string */
export declare function isString(source: unknown): source is string;
export declare function nilToEmptyString(source?: string | null): string;
export declare function notEmptyValue<T>(args: T[]): NonNullable<T> | undefined;
export declare function notEmptyValue<T>(args: T[], defaultValue: NonNullable<T>): NonNullable<T>;
export declare function objectIncludes(b: any, a: any): boolean;
export declare function ownerDocument(node: Node | null | undefined): Document;
export declare function ownerWindow(node: Node | undefined): Window;
declare const PALETTE: {

@@ -102,2 +138,30 @@ reset: string;

export declare function parseDateInput(source: string): ParseDateInputResult | null;
export declare type ParseDateInputResult = {
precision: 'datetime-local';
year: string;
month: string;
date: string;
hours: string;
minutes: string;
source: string;
} | {
precision: 'date';
year: string;
month: string;
date: string;
hours?: undefined;
minutes?: undefined;
source: string;
} | {
precision: 'month';
year: string;
month: string;
date?: undefined;
hours?: undefined;
minutes?: undefined;
source: string;
};
export declare function pushDynamicStyle(styleContent: string): void;

@@ -109,2 +173,4 @@

export declare function safeGetTimeByDateSource(source: number | string | Date): number;
export declare function safeJSONSerializer(replacer?: (this: any, key: string, value: any) => any, cycleReplacer?: (this: any, key: string, value: any) => any): (this: any, key: string, value: any) => any;

@@ -114,4 +180,10 @@

export declare function safeRemainderOperation(a: number, b: number): number;
export declare function toComparableNumbers<ARGS extends number[]>(...args: ARGS): [ARGS, number];
export declare function toFloat(value: string | number): number;
export declare function toHalfWidth(source?: string | null): string;
export declare function toInt(value: string | number): number;

@@ -121,4 +193,6 @@

export declare function toSingleSpace(source?: string | null): string;
export declare type TransitionEventType = 'transitioncancel' | 'transitionend' | 'transitionrun' | 'transitionstart';
export { }

@@ -10,2 +10,20 @@ function toInt(value) {

}
function toComparableNumbers(...args) {
let maxFloating = 0;
args.forEach((arg) => {
const floating = arg.toString().split('.')[1];
if (floating) {
const { length } = floating;
if (length > maxFloating) {
maxFloating = length;
}
}
});
const offset = 10 ** maxFloating;
return [args.map((arg) => arg * offset), offset];
}
function safeRemainderOperation(a, b) {
const [[_a, _b]] = toComparableNumbers(a, b);
return _a % _b;
}

@@ -18,2 +36,18 @@ function isPromise(obj) {

/** Checks if value is string */
function isString(source) {
return typeof source === 'string' || source instanceof String;
}
function nilToEmptyString(source) {
return source == null ? '' : source;
}
function toHalfWidth(source) {
return nilToEmptyString(source).replace(/[!-~]/g, (source) => {
return String.fromCharCode(source.charCodeAt(0) - 0xfee0);
});
}
function toSingleSpace(source) {
return nilToEmptyString(source).replace(/([\s]+)/g, (m) => m.charAt(0));
}
const HAS_SET = typeof Set !== 'undefined';

@@ -29,2 +63,51 @@ function isSet(source) {

/** Escapes regular expression control chars */
function escapeRegExp(str) {
return str.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
}
// cloned from https://github.com/epoberezkin/fast-deep-equal with small changes
function objectIncludes(b, a) {
if (a === b)
return true;
const arrA = Array.isArray(a);
const arrB = Array.isArray(b);
let i;
if (arrA && arrB) {
if (a.length != b.length)
return false;
for (i = 0; i < a.length; i++)
if (!objectIncludes(a[i], b[i]))
return false;
return true;
}
if (arrA != arrB)
return false;
if (a && b && typeof a === 'object' && typeof b === 'object') {
const dateA = a instanceof Date, dateB = b instanceof Date;
if (dateA && dateB)
return a.getTime() == b.getTime();
if (dateA != dateB)
return false;
const regexpA = a instanceof RegExp, regexpB = b instanceof RegExp;
if (regexpA && regexpB)
return a.toString() == b.toString();
if (regexpA != regexpB)
return false;
const keys = Object.keys(a);
// if (keys.length !== Object.keys(b).length) return false;
for (i = 0; i < keys.length; i++)
if (!Object.prototype.hasOwnProperty.call(b, keys[i]))
return false;
for (i = 0; i < keys.length; i++)
if (!objectIncludes(b[keys[i]], a[keys[i]]))
return false;
return true;
}
else if (a && b && typeof a === 'function' && typeof b === 'function') {
return a.toString() === b.toString();
}
return false;
}
function flattenRecursiveArray(source) {

@@ -64,2 +147,57 @@ const results = [];

function safeGetTimeByDateSource(source) {
if (typeof source === 'number')
return source;
if (typeof source === 'string') {
source = new Date(source);
}
return source.getTime();
}
const DATE_INPUT_PRECISIONS = [
'month',
'date',
'datetime-local',
];
const DATE_INPUT_RE = /^(\d{4})-(\d{2})(-(\d{2})(T(\d{2}):(\d{2}))?)?$/;
function parseDateInput(source) {
let result = null;
const matched = source.match(DATE_INPUT_RE);
if (matched) {
const year = matched[1];
const month = matched[2];
const date = matched[4];
const hours = matched[6];
const minutes = matched[7];
if (hours && minutes) {
result = {
precision: 'datetime-local',
year,
month,
date,
hours,
minutes,
source,
};
}
else if (date) {
result = {
precision: 'date',
year,
month,
date,
source,
};
}
else if (year && month) {
result = {
precision: 'month',
year,
month,
source,
};
}
}
return result;
}
function Cloner(opts = {}) {

@@ -288,2 +426,9 @@ const { keyProcesser, valueProcesser } = opts;

}
function ownerDocument(node) {
return (node && node.ownerDocument) || document;
}
function ownerWindow(node) {
const doc = ownerDocument(node);
return doc.defaultView || window;
}

@@ -320,3 +465,3 @@ function resolveDebounceSettings(fnOrSettings, delayOrOptions, immediateOption) {

const { handler, immediate } = resolved;
let { delay = 100 } = resolved;
let { delay = 166 } = resolved;
let booted = false;

@@ -378,2 +523,26 @@ let timerId;

export { Cloner, IN_DOCUMENT, IN_WINDOW, addTransitionEvent, addTransitionendEvent, arrayRemove, attemptFocus, clone, consoleColorString, copyBuffer, debounce, flattenRecursiveArray, focusFirstDescendant, isArrayBufferView, isBuffer, isFocusable, isMap, isPromise, isSet, pushDynamicStyle, resolveDebounceSettings, safeJSONSerializer, safeJSONStringify, toFloat, toInt, toNumber };
/**
* 値が空っぽでないか
*
* 配列の場合はlengthをチェックする
* falseは値として認めない(チェックボックスを考慮)
* value === null
* || value === undefined
* || value === ''
* || Array.isArray(value) && value.length === 0
*/
function isEmpty(value) {
if (typeof value === 'function')
return false;
return value == null || value === false || value.length === 0;
}
function notEmptyValue(args, defaultValue) {
for (const arg of args) {
if (!isEmpty(arg)) {
return arg;
}
}
return defaultValue;
}
export { Cloner, DATE_INPUT_PRECISIONS, DATE_INPUT_RE, IN_DOCUMENT, IN_WINDOW, addTransitionEvent, addTransitionendEvent, arrayRemove, attemptFocus, clone, consoleColorString, copyBuffer, debounce, escapeRegExp, flattenRecursiveArray, focusFirstDescendant, isArrayBufferView, isBuffer, isEmpty, isFocusable, isMap, isPromise, isSet, isString, nilToEmptyString, notEmptyValue, objectIncludes, ownerDocument, ownerWindow, parseDateInput, pushDynamicStyle, resolveDebounceSettings, safeGetTimeByDateSource, safeJSONSerializer, safeJSONStringify, safeRemainderOperation, toComparableNumbers, toFloat, toHalfWidth, toInt, toNumber, toSingleSpace };

2

package.json
{
"name": "@fastkit/helpers",
"version": "0.2.13",
"version": "0.3.0",
"description": "@fastkit/helpers",

@@ -5,0 +5,0 @@ "buildOptions": {

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