This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.write('<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js" onload="callback()"></script>'); | |
function getImageFromUrl(url) { | |
return new Promise((resolve, reject) => { | |
const img = new Image(); | |
img.onError = (err) => reject(err); | |
img.onload = () => resolve(img); | |
img.src = url; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": 3, | |
"sources": [ | |
"utils/selection.ts", | |
"utils/common.ts", | |
"utils/http.ts", | |
"components/button.tsx", | |
"components/input.tsx", | |
"components/date-picker.tsx", | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getFlatObjectDifferencet<T extends object>(newOne: T, oldOne: T, compare: (newOne: T, oldOne: T, key: keyof T) => boolean): Partial<T> { | |
const allKeys: Array<keyof T> = Object.keys(newOne); | |
const uniqueKeys = allKeys.filter((key) => compare(newOne, oldOne, key)); | |
return Object.assign({}, ...uniqueKeys.map(k => ({[k]: newOne[k]}))); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Parser is a function which takes unknown and returns T or null | |
type Parser<T> = (val: unknown) => T | null; | |
// createTypeGuard is a function which takes a parser and returns a new function | |
// The returned function is a safe type guard for T | |
const createTypeGuard = <T>(parse: Parser<T>) => (value: unknown): value is T => { | |
// By assuming that parser returns only T or null | |
// We can say that value from parser is T when it is not a null | |
return parse(value) !== null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const data: unknown = response.body; | |
if (isPerson(data)) { | |
// data is Person | |
console.log('Welcome ' + data.name); | |
} else { | |
console.log('You are not a person!'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createTypeGuard = <T>(parse: (val: unknown) => T | null) => (value: unknown): value is T => { | |
return parse(value) !== null; | |
} | |
const isPerson = createTypeGuard(parsePerson); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const data: unknown = response.body; | |
if (checkType(data, parsePerson)) { | |
// data is Person | |
console.log('Welcome ' + data.name); | |
} else { | |
console.log('You are not a person!'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkType<T>(value: unknown, parse: (val: unknown) => T | null): value is T { | |
return parse(value) !== null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const data: unknown = response.body; | |
const maybePerson = parsePerson(data); | |
if (maybePerson) { | |
// maybePerson is Person | |
console.log('Welcome ' + maybePerson.name); | |
} else { | |
console.log('You are not a person!'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parsePerson(value: unknown): Person | null { | |
if ( | |
typeof value === "object" && | |
value && | |
value.hasOwnProperty("name") && | |
value.hasOwnProperty("age") && | |
value.hasOwnProperty("something") | |
) { | |
const { name, age, something } = value; | |
if ( |
NewerOlder