From dd24f1bb0a4b252e5342b251ba36853364da7b8e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 19 Aug 2021 09:24:29 +0200 Subject: Add video filters to common video pages --- client/src/app/helpers/utils/object.ts | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 client/src/app/helpers/utils/object.ts (limited to 'client/src/app/helpers/utils/object.ts') diff --git a/client/src/app/helpers/utils/object.ts b/client/src/app/helpers/utils/object.ts new file mode 100644 index 000000000..1ca4a23ac --- /dev/null +++ b/client/src/app/helpers/utils/object.ts @@ -0,0 +1,47 @@ +function immutableAssign (target: A, source: B) { + return Object.assign({}, target, source) +} + +function removeElementFromArray (arr: T[], elem: T) { + const index = arr.indexOf(elem) + if (index !== -1) arr.splice(index, 1) +} + +function sortBy (obj: any[], key1: string, key2?: string) { + return obj.sort((a, b) => { + const elem1 = key2 ? a[key1][key2] : a[key1] + const elem2 = key2 ? b[key1][key2] : b[key1] + + if (elem1 < elem2) return -1 + if (elem1 === elem2) return 0 + return 1 + }) +} + +function intoArray (value: any) { + if (!value) return undefined + if (Array.isArray(value)) return value + + if (typeof value === 'string') return value.split(',') + + return [ value ] +} + +function toBoolean (value: any) { + if (!value) return undefined + + if (typeof value === 'boolean') return value + + if (value === 'true') return true + if (value === 'false') return false + + return undefined +} + +export { + sortBy, + immutableAssign, + removeElementFromArray, + intoArray, + toBoolean +} -- cgit v1.2.3