@duely/util
Advanced tools
Comparing version 0.4.10 to 0.4.11
@@ -11,4 +11,4 @@ export declare function timestampToDate(timestamp: number): Date; | ||
export declare function dateToTimestamp(date: undefined): undefined; | ||
declare type DateFormat = 'mmm d, yyyy hh:nn UTC' | 'mmm d, yyyy'; | ||
declare type DateFormat = 'mmm d, yyyy hh:nn UTC' | 'mmm d, yyyy' | 'yyyy-mm-dd'; | ||
export declare function formatDate(date: Date | number, format?: DateFormat): string; | ||
export {}; |
@@ -14,4 +14,4 @@ import { PathValue, SplitString } from '..'; | ||
export declare function mimeTypeFromDataUrl(dataUrl: string): string; | ||
export declare function diff(fromObject: object, omitObject: object): { | ||
[k: string]: any; | ||
export declare function diff<TFrom, TOmit>(fromObject: TFrom, omitObject: TOmit): { | ||
[K in keyof TFrom]: K extends keyof TOmit ? TFrom[K] extends TOmit[K] ? TOmit[K] extends TFrom[K] ? never : TFrom[K] : TFrom[K] : TFrom[K]; | ||
}; | ||
@@ -21,5 +21,6 @@ export declare function get<T, TPath extends string | readonly string[]>(obj: T, path: TPath): TPath extends readonly string[] ? PathValue<T, TPath> : TPath extends string ? PathValue<T, SplitString<TPath, '.'>> : never; | ||
export declare function isPrivateIp(hostname: string): boolean; | ||
export declare function removeUndefined<T extends object>(obj: T): { | ||
[k: string]: any; | ||
export declare function omitUndefined<T extends object>(obj: T): { | ||
[K in keyof T]: T[K] extends undefined ? never : T[K]; | ||
}; | ||
export declare function createUpdateArgs<T extends Record<string, unknown>>(original: T, changed: Partial<T>): { [K_1 in keyof { [K in keyof { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }]: K extends keyof T ? { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }[K] extends T[K] ? T[K] extends { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }[K] ? never : { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }[K] : { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }[K] : { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }[K]; }]: { [K in keyof { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }]: K extends keyof T ? { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }[K] extends T[K] ? T[K] extends { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }[K] ? never : { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }[K] : { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }[K] : { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }[K]; }[K_1] extends undefined ? never : { [K in keyof { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }]: K extends keyof T ? { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }[K] extends T[K] ? T[K] extends { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }[K] ? never : { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }[K] : { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }[K] : { [TKey in keyof T]: TKey extends keyof T ? Partial<T>[TKey] : undefined; }[K]; }[K_1]; }; | ||
export declare function hasOwnProperty<T, TKey extends PropertyKey>(obj: T, propertyName: TKey): obj is T & Record<TKey, unknown>; | ||
@@ -26,0 +27,0 @@ export declare function hasProperty<T, TKey extends PropertyKey>(obj: T, propertyName: TKey): obj is T & Record<TKey, unknown>; |
{ | ||
"name": "@duely/util", | ||
"version": "0.4.10", | ||
"version": "0.4.11", | ||
"description": "Shared utility types and functions used by the Duely APIs.", | ||
@@ -5,0 +5,0 @@ "repository": "github:uoleevi/duely", |
@@ -38,5 +38,13 @@ export function timestampToDate(timestamp: number): Date; | ||
type DateFormat = 'mmm d, yyyy hh:nn UTC' | 'mmm d, yyyy'; | ||
type DateFormat = 'mmm d, yyyy hh:nn UTC' | 'mmm d, yyyy' | 'yyyy-mm-dd'; | ||
export function formatDate(date: Date | number, format?: DateFormat) { | ||
if (date == null) { | ||
return date; | ||
} | ||
if (typeof date === 'string') { | ||
return date; | ||
} | ||
if (typeof date === 'number') { | ||
@@ -50,3 +58,3 @@ if (date === 0) throw Error('Invalid timestamp'); | ||
const dd = String(d).padStart(2, '0'); | ||
const m = date.getUTCMonth(); | ||
const m = date.getUTCMonth() + 1; | ||
const mm = String(m).padStart(2, '0'); | ||
@@ -69,3 +77,22 @@ const mmm = shortMonthNames[m]; | ||
return `${mmm} ${d}, ${yyyy}`; | ||
case 'yyyy-mm-dd': | ||
return `${yyyy}-${mm}-${dd}`; | ||
default: | ||
const formatString = format as string; | ||
let result; | ||
result = formatString.replace('yyyy', yyyy); | ||
result = formatString.replace('nn', nn); | ||
result = formatString.replace('n', n.toString()); | ||
result = formatString.replace('hh', hh); | ||
result = formatString.replace('h', h.toString()); | ||
result = formatString.replace('dd', dd); | ||
result = formatString.replace('d', d.toString()); | ||
result = formatString.replace('mmm', '§'); // temporary placeholder | ||
result = formatString.replace('mm', mm); | ||
result = formatString.replace('m', m.toString()); | ||
result = formatString.replace('§', mmm); | ||
return result; | ||
} | ||
} |
@@ -93,3 +93,14 @@ import { PathValue, SplitString } from '..'; | ||
export function diff(fromObject: object, omitObject: object) { | ||
export function diff<TFrom, TOmit>( | ||
fromObject: TFrom, | ||
omitObject: TOmit | ||
): { | ||
[K in keyof TFrom]: K extends keyof TOmit | ||
? TFrom[K] extends TOmit[K] | ||
? TOmit[K] extends TFrom[K] | ||
? never | ||
: TFrom[K] | ||
: TFrom[K] | ||
: TFrom[K]; | ||
} { | ||
return Object.fromEntries( | ||
@@ -99,3 +110,3 @@ Object.entries(fromObject).filter( | ||
) | ||
); | ||
) as any; | ||
} | ||
@@ -133,6 +144,17 @@ | ||
export function removeUndefined<T extends object>(obj: T) { | ||
return Object.fromEntries(Object.entries(obj).filter((entry) => entry[1] !== undefined)); | ||
export function omitUndefined<T extends object>( | ||
obj: T | ||
): { | ||
[K in keyof T]: T[K] extends undefined ? never : T[K]; | ||
} { | ||
return Object.fromEntries(Object.entries(obj).filter((entry) => entry[1] !== undefined)) as any; | ||
} | ||
export function createUpdateArgs<T extends Record<string, unknown>>( | ||
original: T, | ||
changed: Partial<T> | ||
) { | ||
return omitUndefined(diff(pick(changed, original), original)); | ||
} | ||
export function hasOwnProperty<T, TKey extends PropertyKey>( | ||
@@ -139,0 +161,0 @@ obj: T, |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
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
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
202017
3202