| 1 |
import { SNIPPET_TYPE_SCOPES } from '../../types/Snippet' |
| 2 |
import { isNetworkAdmin } from '../screen' |
| 3 |
import type { Snippet, SnippetScope } from '../../types/Snippet' |
| 4 |
|
| 5 |
const TUPLE_SIZE = 2 |
| 6 |
|
| 7 |
const defaults: Omit<Snippet, 'tags'> = { |
| 8 |
id: 0, |
| 9 |
name: '', |
| 10 |
code: '', |
| 11 |
desc: '', |
| 12 |
scope: 'global', |
| 13 |
modified: '', |
| 14 |
active: false, |
| 15 |
locked: false, |
| 16 |
trashed: false, |
| 17 |
network: isNetworkAdmin(), |
| 18 |
shared_network: null, |
| 19 |
priority: 10, |
| 20 |
conditionId: 0, |
| 21 |
code_error: null, |
| 22 |
code_error_trace: null |
| 23 |
} |
| 24 |
|
| 25 |
const isAbsInt = (value: unknown): value is number => |
| 26 |
'number' === typeof value && 0 < value |
| 27 |
|
| 28 |
const parseStringArray = (value: unknown): string[] | undefined => |
| 29 |
Array.isArray(value) ? value.filter(entry => 'string' === typeof entry) : undefined |
| 30 |
|
| 31 |
const isCodeError = (value: unknown): value is readonly [string, number] => |
| 32 |
Array.isArray(value) && TUPLE_SIZE === value.length && |
| 33 |
'string' === typeof value[0] && 'number' === typeof value[1] |
| 34 |
|
| 35 |
export const isValidScope = (scope: unknown): scope is SnippetScope => |
| 36 |
'string' === typeof scope && Object.values(SNIPPET_TYPE_SCOPES).some(typeScopes => |
| 37 |
typeScopes.some(typeScope => typeScope === scope)) |
| 38 |
|
| 39 |
export const parseSnippetObject = (fields: unknown): Snippet => { |
| 40 |
const result: { -readonly [F in keyof Snippet]: Snippet[F] } = { ...defaults, tags: [] } |
| 41 |
|
| 42 |
if ('object' !== typeof fields || null === fields) { |
| 43 |
return result |
| 44 |
} |
| 45 |
|
| 46 |
return { |
| 47 |
...result, |
| 48 |
...'id' in fields && isAbsInt(fields.id) && { id: fields.id }, |
| 49 |
...'name' in fields && 'string' === typeof fields.name && { name: fields.name }, |
| 50 |
...'desc' in fields && 'string' === typeof fields.desc && { desc: fields.desc }, |
| 51 |
...'code' in fields && 'string' === typeof fields.code && { code: fields.code }, |
| 52 |
...'tags' in fields && { tags: parseStringArray(fields.tags) ?? result.tags }, |
| 53 |
...'scope' in fields && isValidScope(fields.scope) && { scope: fields.scope }, |
| 54 |
...'modified' in fields && 'string' === typeof fields.modified && { modified: fields.modified }, |
| 55 |
...'active' in fields && 'boolean' === typeof fields.active && { active: fields.active }, |
| 56 |
...'locked' in fields && 'boolean' === typeof fields.locked && { locked: fields.locked }, |
| 57 |
...'trashed' in fields && 'boolean' === typeof fields.trashed && { trashed: fields.trashed }, |
| 58 |
...'network' in fields && 'boolean' === typeof fields.network && { network: fields.network }, |
| 59 |
...'shared_network' in fields && 'boolean' === typeof fields.shared_network && { shared_network: fields.shared_network }, |
| 60 |
...'priority' in fields && 'number' === typeof fields.priority && { priority: fields.priority }, |
| 61 |
...'conditionId' in fields && isAbsInt(fields.conditionId) && { conditionId: fields.conditionId }, |
| 62 |
...'condition_id' in fields && isAbsInt(fields.condition_id) && { conditionId: fields.condition_id }, |
| 63 |
...'code_error' in fields && isCodeError(fields.code_error) && { code_error: fields.code_error }, |
| 64 |
...'code_error_trace' in fields && |
| 65 |
('string' === typeof fields.code_error_trace || null === fields.code_error_trace) && { code_error_trace: fields.code_error_trace }, |
| 66 |
...'last_active' in fields && { lastActive: Number(fields.last_active) }, |
| 67 |
} |
| 68 |
} |
| 69 |
|