@xstyled/util
Advanced tools
Comparing version 2.2.3 to 3.0.0-beta.0
@@ -0,94 +1,82 @@ | ||
declare type Path = string | number; | ||
interface ITheme { | ||
[key: string]: any; | ||
[key: number]: any; | ||
} | ||
interface Props<T extends ITheme = ITheme> { | ||
[key: string]: any; | ||
[key: number]: any; | ||
theme?: T; | ||
} | ||
/** | ||
* Identity function. | ||
* @param x | ||
*/ | ||
export declare const identity: <T>(x: T) => T; | ||
declare const identity: <T>(x: T) => T; | ||
/** | ||
* Check if a value is not null and not undefined. | ||
* @param n | ||
*/ | ||
export declare const is: <T>(n: T) => n is Exclude<T, null | undefined>; | ||
declare const is: <T>(n: T) => n is Exclude<T, null | undefined>; | ||
/** | ||
* Check if a value is a number. | ||
* @param n | ||
*/ | ||
export declare const num: (n: any) => n is number; | ||
declare const num: (n: unknown) => n is number; | ||
/** | ||
* Check if a value is a string. | ||
* @param n | ||
*/ | ||
export declare const string: (n: any) => n is string; | ||
declare const string: (n: unknown) => n is string; | ||
/** | ||
* Check if a value is an object. | ||
* @param n | ||
*/ | ||
export declare const obj: (n: any) => n is object; | ||
declare const obj: (n: unknown) => n is { | ||
[key: string]: unknown; | ||
[key: number]: unknown; | ||
}; | ||
/** | ||
* Check if a value is a function. | ||
* @param n | ||
*/ | ||
export declare const func: (n: any) => n is Function; | ||
declare const func: (n: unknown) => n is Function; | ||
/** | ||
* Check if a value is a negative number. | ||
* @param n | ||
*/ | ||
export declare const negative: (n: any) => n is number; | ||
declare type Path = string | number; | ||
declare const negative: (n: unknown) => n is number; | ||
/** | ||
* Get a value from an object or an array. | ||
* @param from | ||
* @param path | ||
*/ | ||
export declare const get: <T>(from: any[] | { | ||
[key: string]: any; | ||
}, path: Path) => T; | ||
declare const get: (from: unknown, path: Path) => unknown; | ||
/** | ||
* Assign object into another | ||
* @param a | ||
* @param b | ||
*/ | ||
export declare const assign: <TObject, TSource>(a: TObject, b: TSource) => TObject & TSource; | ||
declare const assign: <T, U>(target: T, source: U) => T & U; | ||
/** | ||
* Merge deeply one object into another. | ||
* @param a | ||
* @param b | ||
*/ | ||
export declare const merge: <TObject, TSource>(a: TObject, b: TSource) => TObject & TSource; | ||
declare const merge: <T, U>(target: T, source: U) => T & U; | ||
/** | ||
* Warn if a condition is not met. | ||
* @param condition | ||
* @param message | ||
*/ | ||
export declare const warn: (condition: boolean, message: string) => void; | ||
declare const warn: (condition: boolean, message: string) => void; | ||
/** | ||
* Recursively call a function until getting something that is not a function. | ||
* @param value | ||
* @param arg | ||
*/ | ||
export declare function cascade(value: any, arg?: any): Exclude<any, Function>; | ||
declare function cascade(value: unknown, arg?: unknown): Exclude<any, Function>; | ||
/** | ||
* Get value from theme. | ||
* @param props | ||
* @param path | ||
* @param initial | ||
*/ | ||
export declare const getThemeValue: <TProps extends { | ||
theme?: any; | ||
}>(props: TProps, path: Path, initial?: any) => any; | ||
declare const getThemeValue: <T extends Props<ITheme>>(props: T, path: Path, initial?: unknown) => unknown; | ||
/** | ||
* Omit values from an object. | ||
* @param object | ||
* @param values | ||
*/ | ||
export declare function omit<T extends object, K extends string[]>(object: T, values: K): Pick<T, Exclude<keyof T, K[number]>>; | ||
declare function omit<T extends { | ||
[key: string]: unknown; | ||
}, K extends string[]>(object: T, values: K): Pick<T, Exclude<keyof T, K[number]>>; | ||
/** | ||
* Flatten every string together in an array. | ||
* @param array | ||
*/ | ||
export declare function flattenStrings(array: any[]): any[]; | ||
declare function flattenStrings(array: any[]): any[]; | ||
/** | ||
* Flatten an array. | ||
* @param array | ||
*/ | ||
export declare function flatten(array: any[]): any[]; | ||
export {}; | ||
declare function flatten(array: any[]): any[]; | ||
export { ITheme, Path, Props, assign, cascade, flatten, flattenStrings, func, get, getThemeValue, identity, is, merge, negative, num, obj, omit, string, warn }; |
@@ -0,8 +1,111 @@ | ||
'use strict'; | ||
'use strict' | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./util.cjs.production.min.js') | ||
} else { | ||
module.exports = require('./util.cjs.development.js') | ||
const DEV = process.env.NODE_ENV !== "production"; | ||
const identity = (x) => x; | ||
const is = (n) => n !== void 0 && n !== null; | ||
const num = (n) => typeof n === "number" && !Number.isNaN(n); | ||
const string = (n) => typeof n === "string" && n !== ""; | ||
const obj = (n) => typeof n === "object" && n !== null; | ||
const func = (n) => typeof n === "function"; | ||
const negative = (n) => num(n) && n < 0; | ||
const get = (from, path) => { | ||
const paths = String(path).split("."); | ||
const pathsLength = paths.length; | ||
let result = from; | ||
for (let i = 0; i < pathsLength; i += 1) { | ||
if (!is(result)) | ||
return result; | ||
const path2 = paths[i]; | ||
result = is(result[path2]) ? result[path2] : void 0; | ||
} | ||
return result; | ||
}; | ||
const assign = (target, source) => { | ||
if (!is(source)) | ||
return target; | ||
for (const key in source) { | ||
target[key] = source[key]; | ||
} | ||
return target; | ||
}; | ||
const merge = (target, source) => { | ||
if (!is(source)) | ||
return target; | ||
for (const key in source) { | ||
if (obj(target[key])) { | ||
target[key] = merge(assign({}, target[key]), source[key]); | ||
} else { | ||
target[key] = source[key]; | ||
} | ||
} | ||
return target; | ||
}; | ||
const warn = (condition, message) => { | ||
if (DEV) { | ||
if (!condition && console.error) { | ||
console.error(message); | ||
} | ||
} | ||
}; | ||
function cascade(value, arg) { | ||
if (typeof value === "function") { | ||
return cascade(value(arg), arg); | ||
} | ||
return value; | ||
} | ||
const getThemeValue = (props, path, initial = props.theme) => cascade(get(initial, path), props); | ||
function omit(object, values) { | ||
const result = {}; | ||
for (const key in object) { | ||
if (values.indexOf(key) === -1) { | ||
result[key] = object[key]; | ||
} | ||
} | ||
return result; | ||
} | ||
function flattenStrings(array) { | ||
return array.reduce((flattenedArray, value) => { | ||
const lastIndex = flattenedArray.length - 1; | ||
const last = flattenedArray[lastIndex]; | ||
if (typeof last === "string" && typeof value === "string") { | ||
flattenedArray[lastIndex] = last + value; | ||
} else { | ||
flattenedArray.push(value); | ||
} | ||
return flattenedArray; | ||
}, []); | ||
} | ||
function flattenDown(array, result) { | ||
for (let i = 0; i < array.length; i++) { | ||
const value = array[i]; | ||
if (Array.isArray(value)) { | ||
flattenDown(value, result); | ||
} else { | ||
result.push(value); | ||
} | ||
} | ||
return result; | ||
} | ||
function flatten(array) { | ||
return flattenDown(array, []); | ||
} | ||
exports.assign = assign; | ||
exports.cascade = cascade; | ||
exports.flatten = flatten; | ||
exports.flattenStrings = flattenStrings; | ||
exports.func = func; | ||
exports.get = get; | ||
exports.getThemeValue = getThemeValue; | ||
exports.identity = identity; | ||
exports.is = is; | ||
exports.merge = merge; | ||
exports.negative = negative; | ||
exports.num = num; | ||
exports.obj = obj; | ||
exports.omit = omit; | ||
exports.string = string; | ||
exports.warn = warn; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@xstyled/util", | ||
"description": "xstyled common utilities.", | ||
"version": "2.2.3", | ||
"version": "3.0.0-beta.0", | ||
"sideEffects": false, | ||
"main": "dist/index.js", | ||
"module": "dist/util.esm.js", | ||
"module": "dist/index.mjs", | ||
"typings": "dist/index.d.ts", | ||
@@ -12,5 +12,4 @@ "author": "Greg Bergé <berge.greg@gmail.com>", | ||
"scripts": { | ||
"reset": "rm -rf dist && rm -rf node_modules/.cache", | ||
"watch": "tsdx watch", | ||
"build": "tsdx build", | ||
"reset": "rm -rf dist", | ||
"build": "../../node_modules/.bin/rollup -c ../../build/rollup.config.js", | ||
"prepublishOnly": "yarn reset && yarn build" | ||
@@ -27,3 +26,3 @@ }, | ||
}, | ||
"gitHead": "9cec90b9574162148b5e4cbd7b8727d3e12ac22f" | ||
"gitHead": "9ef8a37adac6d421956b2ae1c00c0651d3d6e835" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
1
23771
9
279
1
1