Skip to content

Instantly share code, notes, and snippets.

View gregpalaci's full-sized avatar

Greg Palaci gregpalaci

  • Cactus & Dove Ltd.
  • London, UK
View GitHub Profile
View nwjs heap
Analysis of sampling nwjs Helper (Renderer) (pid 61391) every 1 millisecond
Process: nwjs Helper (Renderer) [61391]
Path: /Applications/Popcorn-Time 15.app/Contents/Frameworks/nwjs Framework.framework/Versions/119.0.6045.105/Helpers/nwjs Helper (Renderer).app/Contents/MacOS/nwjs Helper (Renderer)
Load Address: 0x104b80000
Identifier: nwjs Helper (Renderer)
Version: ???
Code Type: ARM64
Platform: macOS
Parent Process: ??? [1]
@gregpalaci
gregpalaci / doAwait.js
Created January 15, 2024 19:23
await without catch
View doAwait.js
const doAwait = async (promise) => {
try {
const data = await promise;
return [undefined, data];
} catch (error) {
return [error, undefined];
}
};
const [error, result] = await doAwait(myPromise);
@gregpalaci
gregpalaci / tryCatch.ts
Last active November 11, 2023 14:16
tryCatch.ts
View tryCatch.ts
type TryCatchProps<T> = {
tryFn: () => T;
catchFn: (error: unknown) => T;
}
function tryCatch<T>({ tryFn, catchFn }: TryCatchProps<T>): T {
try {
return tryFn();
} catch (error) {
return catchFn(error);
@gregpalaci
gregpalaci / jsonSchema.ts
Created November 3, 2023 00:38
jsonSchema.ts
View jsonSchema.ts
const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]);
type Literal = z.infer<typeof literalSchema>;
type Json = Literal | { [key: string]: Json } | Json[];
const jsonSchema: z.ZodType<Json> = z.lazy(() =>
z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)])
);
jsonSchema.parse(data);
@gregpalaci
gregpalaci / safeJsonParse.ts
Created November 2, 2023 20:10
safeJsonParse
View safeJsonParse.ts
const safeJsonParse = <T>(str: string) => {
try {
const jsonValue: T = JSON.parse(str);
return jsonValue;
} catch {
return undefined;
}
};
@gregpalaci
gregpalaci / isPromise.js
Created October 16, 2023 17:37
isPromise.js
View isPromise.js
/**
* Determine whether the given `promise` is a Promise.
*
* @param {*} promise
*
* @returns {Boolean}
*/
function isPromise(promise) {
return !!promise && typeof promise.then === 'function'
}
@gregpalaci
gregpalaci / isNumericString.js
Created October 4, 2023 17:43
Ensure a String is Numeric in JavaScript
View isNumericString.js
/**
* Determine whether the given `input` is a number.
*
* @param {String} input
*
* @returns {Boolean}
*/
const isNumericString = (input) => typeof input === 'string' && !Number.isNaN(input)
@gregpalaci
gregpalaci / ucFirst.js
Created October 4, 2023 17:42
Capitalize the First Letter of a String in JavaScript
View ucFirst.js
/**
* Uppercases the first character in the `string`.
*
* @param {String} string
*
* @returns {String}
*/
function ucFirst (string) {
if (!(typeof string !== 'string')) {
return ''
@gregpalaci
gregpalaci / isObject.js
Created October 4, 2023 17:40
isObject
View isObject.js
const isObject = (value) => Object.prototype.toString.call(value) === '[object Object]'
@gregpalaci
gregpalaci / lowerCaseKeys.js
Created October 4, 2023 17:39
Lowercase All Keys of an Object in JavaScript
View lowerCaseKeys.js
/**
* Lowercase all top-level keys of the given `object` to lowercase.
*
* @returns {Object}
*/
function lowercaseKeys(obj) {
if (!isObject(obj)) {
throw new Error(`You must provide an object to "lowercaseKeys". Received "${typeof obj}"`)
}