Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c88d96bcfa | |||
| 057528b0c5 | |||
| 1595aea529 | |||
| 08503de552 | |||
| a9a9b04a5a |
@@ -13,3 +13,7 @@ quote_type = single
|
|||||||
[*.md]
|
[*.md]
|
||||||
max_line_length = off
|
max_line_length = off
|
||||||
trim_trailing_whitespace = false
|
trim_trailing_whitespace = false
|
||||||
|
|
||||||
|
[*.{yaml,yml}]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
||||||
96
.github/workflows/build.yaml
vendored
96
.github/workflows/build.yaml
vendored
@@ -11,26 +11,26 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
container: node:20-alpine
|
container: node:20-alpine
|
||||||
steps:
|
steps:
|
||||||
- name: Clone Repository
|
- name: Clone Repository
|
||||||
uses: ztimson/actions/clone@develop
|
uses: ztimson/actions/clone@develop
|
||||||
|
|
||||||
- name: Install & Build
|
- name: Install & Build
|
||||||
run: |
|
run: |
|
||||||
npm i
|
npm i
|
||||||
npm run build
|
npm run build
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: npm run test:coverage
|
run: npm run test:coverage
|
||||||
|
|
||||||
- name: Upload to Registry
|
- name: Upload to Registry
|
||||||
uses: ztimson/actions/npm/publish@develop
|
uses: ztimson/actions/npm/publish@develop
|
||||||
|
|
||||||
- name: Upload to NPM
|
- name: Upload to NPM
|
||||||
uses: ztimson/actions/npm/publish@develop
|
uses: ztimson/actions/npm/publish@develop
|
||||||
with:
|
with:
|
||||||
owner: ztimson
|
owner: ztimson
|
||||||
registry: https://registry.npmjs.org/
|
registry: https://registry.npmjs.org/
|
||||||
token: ${{secrets.NPM_TOKEN}}
|
token: ${{secrets.NPM_TOKEN}}
|
||||||
tag:
|
tag:
|
||||||
name: Tag Version
|
name: Tag Version
|
||||||
needs: build
|
needs: build
|
||||||
@@ -38,23 +38,53 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
container: node
|
container: node
|
||||||
steps:
|
steps:
|
||||||
- name: Clone Repository
|
- name: Clone Repository
|
||||||
uses: ztimson/actions/clone@develop
|
uses: ztimson/actions/clone@develop
|
||||||
|
|
||||||
- name: Get Version Number
|
|
||||||
run: echo "VERSION=$(cat package.json | grep version | grep -Eo ':.+' | grep -Eo '[[:alnum:]\.\/\-]+')" >> $GITHUB_ENV
|
|
||||||
|
|
||||||
- name: Tag Version
|
|
||||||
uses: ztimson/actions/tag@develop
|
|
||||||
with:
|
|
||||||
tag: ${{env.VERSION}}
|
|
||||||
|
|
||||||
|
- name: Get Version Number
|
||||||
|
run: echo "VERSION=$(cat package.json | grep version | grep -Eo ':.+' | grep -Eo '[[:alnum:]\.\/\-]+')" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Tag Version
|
||||||
|
uses: ztimson/actions/tag@develop
|
||||||
|
with:
|
||||||
|
tag: ${{env.VERSION}}
|
||||||
docs:
|
docs:
|
||||||
name: Publish Documentation
|
name: Publish Docs
|
||||||
needs: build
|
runs-on: ubuntu-latest
|
||||||
uses: ztimson/actions/.github/workflows/docker.yaml@develop
|
container: docker:dind
|
||||||
with:
|
steps:
|
||||||
name: ztimson/utils
|
- name: Clone Repository
|
||||||
repository: ${{github.server_url}}/${{github.repository}}.git
|
uses: ztimson/actions/clone@develop
|
||||||
pass: ${{secrets.DEPLOY_TOKEN}}
|
|
||||||
|
- name: Build Image
|
||||||
|
run: |
|
||||||
|
REGISTRY=$(echo ${{github.server_url}} | sed s%http://%% | sed s%https://%%)
|
||||||
|
docker build -t "$REGISTRY/${{github.repository}}:${{github.ref_name}}" .
|
||||||
|
|
||||||
|
- name: Build Image
|
||||||
|
run: |
|
||||||
|
echo "CHECKSUM: ${{env.CHECKSUM}}"
|
||||||
|
REGISTRY=$(echo ${{github.server_url}} | sed s%http://%% | sed s%https://%%)
|
||||||
|
docker build -t "$REGISTRY/${{github.repository}}:${{github.ref_name}}" --build-arg LICENSE_KEY="${{secrets.LICENSE_KEY}}" --build-arg UPDATE_TOKEN="${{secrets.UPDATE_TOKEN}}" .
|
||||||
|
|
||||||
|
- name: Publish Branch Tag
|
||||||
|
run: |
|
||||||
|
REGISTRY=$(echo ${{github.server_url}} | sed s%http://%% | sed s%https://%%)
|
||||||
|
docker login -u ${{github.repository_owner}} -p ${{secrets.DEPLOY_TOKEN}} $REGISTRY
|
||||||
|
docker push "$REGISTRY/${{github.repository}}:${{github.ref_name}}"
|
||||||
|
|
||||||
|
- name: Publish Version Tag
|
||||||
|
run: |
|
||||||
|
if [ "${{github.ref_name}}" = "master" ]; then
|
||||||
|
REGISTRY=$(echo ${{github.server_url}} | sed s%http://%% | sed s%https://%%)
|
||||||
|
docker tag "$REGISTRY/${{github.repository}}:${{github.ref_name}}" "$REGISTRY/${{github.repository}}:${{env.VERSION}}"
|
||||||
|
docker push "$REGISTRY/${{github.repository}}:${{env.VERSION}}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Publish Latest Tag
|
||||||
|
run: |
|
||||||
|
if [ "${{github.ref_name}}" = "master" ]; then
|
||||||
|
REGISTRY=$(echo ${{github.server_url}} | sed s%http://%% | sed s%https://%%)
|
||||||
|
docker tag "$REGISTRY/${{github.repository}}:${{github.ref_name}}" "$REGISTRY/${{github.repository}}:latest"
|
||||||
|
docker push "$REGISTRY/${{github.repository}}:latest"
|
||||||
|
fi
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.27.8",
|
"version": "0.27.10",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
82
src/cache.ts
82
src/cache.ts
@@ -1,5 +1,6 @@
|
|||||||
import {Database, Table} from './database.ts';
|
import {Database, Table} from './database.ts';
|
||||||
import {deepCopy, includes, JSONSanitize} from './objects.ts';
|
import {JSONSanitize} from './json.ts';
|
||||||
|
import {deepCopy, includes} from './objects.ts';
|
||||||
|
|
||||||
export type CacheOptions = {
|
export type CacheOptions = {
|
||||||
/** Delete keys automatically after x amount of seconds */
|
/** Delete keys automatically after x amount of seconds */
|
||||||
@@ -84,6 +85,7 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
return <K>(value as any)[this.key];
|
return <K>(value as any)[this.key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Save item to storage */
|
||||||
private save(key?: K) {
|
private save(key?: K) {
|
||||||
const persists: {storage: any, key: string} = <any>this.options.persistentStorage;
|
const persists: {storage: any, key: string} = <any>this.options.persistentStorage;
|
||||||
if(!!persists?.storage) {
|
if(!!persists?.storage) {
|
||||||
@@ -130,16 +132,13 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
const out: CachedValue<T>[] = [];
|
const out: CachedValue<T>[] = [];
|
||||||
for(const v of this.store.values()) {
|
for(const v of this.store.values()) {
|
||||||
const val: any = v;
|
const val: any = v;
|
||||||
if(expired || !val?._expired) out.push(deepCopy<any>(val));
|
if(expired || !val?._expired) out.push(deepCopy(val));
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a new item to the cache. Like set, but finds key automatically
|
* Add a new item to the cache. Like set, but finds key automatically
|
||||||
* @param {T} value Item to add to cache
|
|
||||||
* @param {number | undefined} ttl Override default expiry
|
|
||||||
* @return {this}
|
|
||||||
*/
|
*/
|
||||||
add(value: T, ttl = this.ttl): this {
|
add(value: T, ttl = this.ttl): this {
|
||||||
const key = this.getKey(value);
|
const key = this.getKey(value);
|
||||||
@@ -149,9 +148,6 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Add several rows to the cache
|
* Add several rows to the cache
|
||||||
* @param {T[]} rows Several items that will be cached using the default key
|
|
||||||
* @param complete Mark cache as complete & reliable, defaults to true
|
|
||||||
* @return {this}
|
|
||||||
*/
|
*/
|
||||||
addAll(rows: T[], complete = true): this {
|
addAll(rows: T[], complete = true): this {
|
||||||
this.clear();
|
this.clear();
|
||||||
@@ -160,9 +156,7 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Remove all keys */
|
||||||
* Remove all keys from cache
|
|
||||||
*/
|
|
||||||
clear(): this {
|
clear(): this {
|
||||||
this.complete = false;
|
this.complete = false;
|
||||||
for (const [k, t] of this.timers) clearTimeout(t);
|
for (const [k, t] of this.timers) clearTimeout(t);
|
||||||
@@ -173,10 +167,7 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Delete a cached item */
|
||||||
* Delete an item from the cache
|
|
||||||
* @param {K} key Item's primary key
|
|
||||||
*/
|
|
||||||
delete(key: K): this {
|
delete(key: K): this {
|
||||||
this.clearTimer(key);
|
this.clearTimer(key);
|
||||||
const idx = this.lruOrder.indexOf(key);
|
const idx = this.lruOrder.indexOf(key);
|
||||||
@@ -186,23 +177,17 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Return entries as array */
|
||||||
* Return cache as an array of key-value pairs
|
|
||||||
* @return {[K, T][]} Key-value pairs array
|
|
||||||
*/
|
|
||||||
entries(expired?: boolean): [K, CachedValue<T>][] {
|
entries(expired?: boolean): [K, CachedValue<T>][] {
|
||||||
const out: [K, CachedValue<T>][] = [];
|
const out: [K, CachedValue<T>][] = [];
|
||||||
for(const [k, v] of this.store.entries()) {
|
for(const [k, v] of this.store.entries()) {
|
||||||
const val: any = v;
|
const val: any = v;
|
||||||
if(expired || !val?._expired) out.push([k, deepCopy<any>(val)]);
|
if(expired || !val?._expired) out.push([k, deepCopy(val)]);
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Manually expire a cached item */
|
||||||
* Manually expire a cached item
|
|
||||||
* @param {K} key Key to expire
|
|
||||||
*/
|
|
||||||
expire(key: K): this {
|
expire(key: K): this {
|
||||||
this.complete = false;
|
this.complete = false;
|
||||||
if(this.options.expiryPolicy == 'keep') {
|
if(this.options.expiryPolicy == 'keep') {
|
||||||
@@ -216,39 +201,26 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Find first matching item */
|
||||||
* Find the first cached item to match a filter
|
|
||||||
* @param {Partial<T>} filter Partial item to match
|
|
||||||
* @param {Boolean} expired Include expired items, defaults to false
|
|
||||||
* @returns {T | undefined} Cached item or undefined if nothing matched
|
|
||||||
*/
|
|
||||||
find(filter: Partial<T>, expired?: boolean): T | undefined {
|
find(filter: Partial<T>, expired?: boolean): T | undefined {
|
||||||
for(const v of this.store.values()) {
|
for(const v of this.store.values()) {
|
||||||
const row: any = v;
|
const row: any = v;
|
||||||
if((expired || !row._expired) && includes(row, filter)) return deepCopy<any>(row);
|
if((expired || !row._expired) && includes(row, filter)) return deepCopy(row);
|
||||||
}
|
}
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Get cached item by key */
|
||||||
* Get item from the cache
|
|
||||||
* @param {K} key Key to lookup
|
|
||||||
* @param expired Include expired items
|
|
||||||
* @return {T} Cached item
|
|
||||||
*/
|
|
||||||
get(key: K, expired?: boolean): CachedValue<T> | null {
|
get(key: K, expired?: boolean): CachedValue<T> | null {
|
||||||
const raw = this.store.get(key);
|
const raw = this.store.get(key);
|
||||||
if(raw == null) return null;
|
if(raw == null) return null;
|
||||||
const cached: any = deepCopy<any>(raw);
|
|
||||||
this.touchLRU(key);
|
this.touchLRU(key);
|
||||||
if(expired || !cached?._expired) return cached;
|
const isExpired = (raw as any)?._expired;
|
||||||
|
if(expired || !isExpired) return deepCopy(raw);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Return list of keys */
|
||||||
* Get a list of cached keys
|
|
||||||
* @return {K[]} Array of keys
|
|
||||||
*/
|
|
||||||
keys(expired?: boolean): K[] {
|
keys(expired?: boolean): K[] {
|
||||||
const out: K[] = [];
|
const out: K[] = [];
|
||||||
for(const [k, v] of this.store.entries()) {
|
for(const [k, v] of this.store.entries()) {
|
||||||
@@ -258,26 +230,17 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Return map of key → item */
|
||||||
* Get map of cached items
|
|
||||||
* @return {Record<K, T>}
|
|
||||||
*/
|
|
||||||
map(expired?: boolean): Record<K, CachedValue<T>> {
|
map(expired?: boolean): Record<K, CachedValue<T>> {
|
||||||
const copy: any = {};
|
const copy: any = {};
|
||||||
for(const [k, v] of this.store.entries()) {
|
for(const [k, v] of this.store.entries()) {
|
||||||
const val: any = v;
|
const val: any = v;
|
||||||
if(expired || !val?._expired) copy[k as any] = deepCopy<any>(val);
|
if(expired || !val?._expired) copy[k as any] = deepCopy(val);
|
||||||
}
|
}
|
||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Add item manually specifying the key */
|
||||||
* Add an item to the cache manually specifying the key
|
|
||||||
* @param {K} key Key item will be cached under
|
|
||||||
* @param {T} value Item to cache
|
|
||||||
* @param {number | undefined} ttl Override default expiry in seconds
|
|
||||||
* @return {this}
|
|
||||||
*/
|
|
||||||
set(key: K, value: T, ttl = this.options.ttl): this {
|
set(key: K, value: T, ttl = this.options.ttl): this {
|
||||||
if(this.options.expiryPolicy == 'keep') delete (<any>value)._expired;
|
if(this.options.expiryPolicy == 'keep') delete (<any>value)._expired;
|
||||||
this.clearTimer(key);
|
this.clearTimer(key);
|
||||||
@@ -288,15 +251,12 @@ export class Cache<K extends string | number | symbol, T> {
|
|||||||
const t = setTimeout(() => {
|
const t = setTimeout(() => {
|
||||||
this.expire(key);
|
this.expire(key);
|
||||||
this.save(key);
|
this.save(key);
|
||||||
}, (ttl || 0) * 1000);
|
}, ttl * 1000);
|
||||||
this.timers.set(key, t);
|
this.timers.set(key, t);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Get all cached items */
|
||||||
* Get all cached items
|
values = this.all;
|
||||||
* @return {T[]} Array of items
|
|
||||||
*/
|
|
||||||
values = this.all
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import {makeArray} from './array.ts';
|
import {makeArray} from './array.ts';
|
||||||
import {ASet} from './aset.ts';
|
import {ASet} from './aset.ts';
|
||||||
import {dotNotation, flattenObj, JSONSanitize} from './objects.ts';
|
import {JSONSanitize} from './json.ts';
|
||||||
|
import {dotNotation, flattenObj} from './objects.ts';
|
||||||
import {LETTER_LIST} from './string.ts';
|
import {LETTER_LIST} from './string.ts';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import {makeArray} from './array.ts';
|
import {makeArray} from './array.ts';
|
||||||
import {JSONAttemptParse} from './objects.ts';
|
import {JSONAttemptParse} from './json.ts';
|
||||||
import {PromiseProgress} from './promise-progress';
|
import {PromiseProgress} from './promise-progress';
|
||||||
import {formatDate} from './time.ts';
|
import {formatDate} from './time.ts';
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ export * from './files';
|
|||||||
export * from './emitter';
|
export * from './emitter';
|
||||||
export * from './errors';
|
export * from './errors';
|
||||||
export * from './http';
|
export * from './http';
|
||||||
|
export * from './json';
|
||||||
export * from './jwt';
|
export * from './jwt';
|
||||||
export * from './logger';
|
export * from './logger';
|
||||||
export * from './math';
|
export * from './math';
|
||||||
|
|||||||
40
src/json.ts
Normal file
40
src/json.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/**
|
||||||
|
* Parse JSON but return the original string if it fails
|
||||||
|
*
|
||||||
|
* @param {any} json JSON string to parse
|
||||||
|
* @param fallback Fallback value if unable to parse, defaults to original string
|
||||||
|
* @return {string | T} Object if successful, original string otherwise
|
||||||
|
*/
|
||||||
|
export function JSONAttemptParse<T1, T2>(json: T2, fallback?: T1): T1 | T2 {
|
||||||
|
try { return JSON.parse(<any>json); }
|
||||||
|
catch { return fallback ?? json; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stringifies objects & skips primitives
|
||||||
|
*
|
||||||
|
* @param {any} obj Object to convert to serializable value
|
||||||
|
* @return {string | T} Serialized value
|
||||||
|
*/
|
||||||
|
export function JSONSerialize<T1>(obj: T1): T1 | string {
|
||||||
|
if(typeof obj == 'object' && obj != null) return JSONSanitize(obj);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert an object to a JSON string avoiding any circular references.
|
||||||
|
*
|
||||||
|
* @param obj Object to convert to JSON
|
||||||
|
* @param {number} space Format the JSON with spaces
|
||||||
|
* @return {string} JSON string
|
||||||
|
*/
|
||||||
|
export function JSONSanitize(obj: any, space?: number): string {
|
||||||
|
const cache: any[] = [];
|
||||||
|
return JSON.stringify(obj, (key, value) => {
|
||||||
|
if(typeof value === 'object' && value !== null) {
|
||||||
|
if(cache.includes(value)) return '[Circular]';
|
||||||
|
cache.push(value);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}, space);
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import {JSONAttemptParse} from './objects.ts';
|
import {JSONAttemptParse} from './json.ts';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a JSON Web Token (JWT) using the provided payload.
|
* Creates a JSON Web Token (JWT) using the provided payload.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import {TypedEmitter, TypedEvents} from './emitter';
|
import {TypedEmitter, TypedEvents} from './emitter';
|
||||||
import {JSONSanitize} from './objects.ts';
|
import {JSONSanitize} from './json.ts';
|
||||||
|
|
||||||
export const CliEffects = {
|
export const CliEffects = {
|
||||||
CLEAR: "\x1b[0m",
|
CLEAR: "\x1b[0m",
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
export type Delta = { [key: string]: any | Delta | null };
|
import {JSONSanitize} from './json.ts';
|
||||||
|
|
||||||
|
export type Delta = { [key: string]: any | Delta | null };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Applies deltas to `target`.
|
* Applies deltas to `target`.
|
||||||
@@ -81,6 +82,9 @@ export function clean<T>(obj: T, undefinedOnly = false): Partial<T> {
|
|||||||
* @returns {T} Type
|
* @returns {T} Type
|
||||||
*/
|
*/
|
||||||
export function deepCopy<T>(value: T): T {
|
export function deepCopy<T>(value: T): T {
|
||||||
|
if(value == null) return value;
|
||||||
|
const t = typeof value;
|
||||||
|
if(t === 'string' || t === 'number' || t === 'boolean' || t === 'function') return value;
|
||||||
try {return structuredClone(value); }
|
try {return structuredClone(value); }
|
||||||
catch { return JSON.parse(JSONSanitize(value)); }
|
catch { return JSON.parse(JSONSanitize(value)); }
|
||||||
}
|
}
|
||||||
@@ -303,43 +307,3 @@ export function objectMap<T>(obj: any, fn: (key: string, value: any) => any): T
|
|||||||
return <any>Object.entries(obj).map(([key, value]: [string, any]) => [key, fn(key, value)])
|
return <any>Object.entries(obj).map(([key, value]: [string, any]) => [key, fn(key, value)])
|
||||||
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});
|
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse JSON but return the original string if it fails
|
|
||||||
*
|
|
||||||
* @param {any} json JSON string to parse
|
|
||||||
* @return {string | T} Object if successful, original string otherwise
|
|
||||||
*/
|
|
||||||
export function JSONAttemptParse<T1, T2>(json: T2): T1 | T2 {
|
|
||||||
try { return JSON.parse(<any>json); }
|
|
||||||
catch { return json; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stringifies objects & skips primitives
|
|
||||||
*
|
|
||||||
* @param {any} obj Object to convert to serializable value
|
|
||||||
* @return {string | T} Serialized value
|
|
||||||
*/
|
|
||||||
export function JSONSerialize<T1>(obj: T1): T1 | string {
|
|
||||||
if(typeof obj == 'object' && obj != null) return JSONSanitize(obj);
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert an object to a JSON string avoiding any circular references.
|
|
||||||
*
|
|
||||||
* @param obj Object to convert to JSON
|
|
||||||
* @param {number} space Format the JSON with spaces
|
|
||||||
* @return {string} JSON string
|
|
||||||
*/
|
|
||||||
export function JSONSanitize(obj: any, space?: number): string {
|
|
||||||
const cache: any[] = [];
|
|
||||||
return JSON.stringify(obj, (key, value) => {
|
|
||||||
if(typeof value === 'object' && value !== null) {
|
|
||||||
if(cache.includes(value)) return '[Circular]';
|
|
||||||
cache.push(value);
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}, space);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import {dotNotation, JSONAttemptParse, JSONSerialize} from './objects.ts';
|
import {JSONAttemptParse, JSONSerialize} from './json.ts';
|
||||||
|
import {dotNotation} from './objects.ts';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filters an array of objects based on a search term and optional regex checking.
|
* Filters an array of objects based on a search term and optional regex checking.
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
import {numSuffix} from './math.ts';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Like setInterval but will adjust the timeout value to account for runtime
|
* Like setInterval but will adjust the timeout value to account for runtime
|
||||||
* @param {Function} cb Callback function that will be ran
|
* @param {Function} cb Callback function that will be ran
|
||||||
|
|||||||
33
tests/json.spec.ts
Normal file
33
tests/json.spec.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import {JSONAttemptParse, JSONSanitize, JSONSerialize} from '../src';
|
||||||
|
|
||||||
|
describe('JSON Utilities', () => {
|
||||||
|
describe('JSONAttemptParse', () => {
|
||||||
|
it('parses valid JSON', () => {
|
||||||
|
expect(JSONAttemptParse('{"a":1}')).toEqual({ a: 1 });
|
||||||
|
});
|
||||||
|
it('returns original string on error', () => {
|
||||||
|
expect(JSONAttemptParse('not json')).toBe('not json');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('JSONSerialize', () => {
|
||||||
|
it('serializes objects', () => {
|
||||||
|
expect(JSONSerialize({ a: 1 })).toBe(JSON.stringify({ a: 1 }));
|
||||||
|
});
|
||||||
|
it('leaves primitives as is', () => {
|
||||||
|
expect(JSONSerialize('test')).toBe('test');
|
||||||
|
expect(JSONSerialize(123)).toBe(123);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('JSONSanitize', () => {
|
||||||
|
it('stringifies objects', () => {
|
||||||
|
expect(JSONSanitize({ a: 1 })).toBe(JSON.stringify({ a: 1 }));
|
||||||
|
});
|
||||||
|
it('does not throw on circular refs', () => {
|
||||||
|
const obj: any = {};
|
||||||
|
obj.self = obj;
|
||||||
|
expect(() => JSONSanitize(obj)).not.toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
clean, deepCopy, deepMerge, dotNotation, encodeQuery, flattenObj, formData, includes, isEqual, mixin,
|
clean, deepCopy, deepMerge, dotNotation, encodeQuery, flattenObj, formData, includes, isEqual, mixin,
|
||||||
JSONAttemptParse, JSONSerialize, JSONSanitize
|
|
||||||
} from '../src';
|
} from '../src';
|
||||||
|
|
||||||
describe('Object utilities', () => {
|
describe('Object utilities', () => {
|
||||||
@@ -131,34 +130,4 @@ describe('Object utilities', () => {
|
|||||||
expect(c.bar()).toBe(2);
|
expect(c.bar()).toBe(2);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('JSONAttemptParse', () => {
|
|
||||||
it('parses valid JSON', () => {
|
|
||||||
expect(JSONAttemptParse('{"a":1}')).toEqual({ a: 1 });
|
|
||||||
});
|
|
||||||
it('returns original string on error', () => {
|
|
||||||
expect(JSONAttemptParse('not json')).toBe('not json');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('JSONSerialize', () => {
|
|
||||||
it('serializes objects', () => {
|
|
||||||
expect(JSONSerialize({ a: 1 })).toBe(JSON.stringify({ a: 1 }));
|
|
||||||
});
|
|
||||||
it('leaves primitives as is', () => {
|
|
||||||
expect(JSONSerialize('test')).toBe('test');
|
|
||||||
expect(JSONSerialize(123)).toBe(123);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('JSONSanitize', () => {
|
|
||||||
it('stringifies objects', () => {
|
|
||||||
expect(JSONSanitize({ a: 1 })).toBe(JSON.stringify({ a: 1 }));
|
|
||||||
});
|
|
||||||
it('does not throw on circular refs', () => {
|
|
||||||
const obj: any = {};
|
|
||||||
obj.self = obj;
|
|
||||||
expect(() => JSONSanitize(obj)).not.toThrow();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user