|
|
@ -17,7 +17,6 @@ |
|
|
import _ from 'lodash'; |
|
|
import _ from 'lodash'; |
|
|
import { Observable, Subject } from 'rxjs'; |
|
|
import { Observable, Subject } from 'rxjs'; |
|
|
import { finalize, share } from 'rxjs/operators'; |
|
|
import { finalize, share } from 'rxjs/operators'; |
|
|
import base64js from 'base64-js'; |
|
|
|
|
|
import { Datasource } from '@app/shared/models/widget.models'; |
|
|
import { Datasource } from '@app/shared/models/widget.models'; |
|
|
|
|
|
|
|
|
const varsRegex = /\${([^}]*)}/g; |
|
|
const varsRegex = /\${([^}]*)}/g; |
|
|
@ -123,7 +122,8 @@ export function isEmpty(obj: any): boolean { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export function formatValue(value: any, dec?: number, units?: string, showZeroDecimals?: boolean): string | undefined { |
|
|
export function formatValue(value: any, dec?: number, units?: string, showZeroDecimals?: boolean): string | undefined { |
|
|
if (isDefinedAndNotNull(value) && isNumeric(value) && (isDefinedAndNotNull(dec) || isDefinedAndNotNull(units) || Number(value).toString() === value)) { |
|
|
if (isDefinedAndNotNull(value) && isNumeric(value) && |
|
|
|
|
|
(isDefinedAndNotNull(dec) || isDefinedAndNotNull(units) || Number(value).toString() === value)) { |
|
|
let formatted: string | number = Number(value); |
|
|
let formatted: string | number = Number(value); |
|
|
if (isDefinedAndNotNull(dec)) { |
|
|
if (isDefinedAndNotNull(dec)) { |
|
|
formatted = formatted.toFixed(dec); |
|
|
formatted = formatted.toFixed(dec); |
|
|
@ -164,28 +164,21 @@ export function deleteNullProperties(obj: any) { |
|
|
|
|
|
|
|
|
export function objToBase64(obj: any): string { |
|
|
export function objToBase64(obj: any): string { |
|
|
const json = JSON.stringify(obj); |
|
|
const json = JSON.stringify(obj); |
|
|
const encoded = utf8Encode(json); |
|
|
return btoa(encodeURIComponent(json).replace(/%([0-9A-F]{2})/g, |
|
|
return base64js.fromByteArray(encoded); |
|
|
function toSolidBytes(match, p1) { |
|
|
|
|
|
return String.fromCharCode(Number('0x' + p1)); |
|
|
|
|
|
})); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export function base64toObj(b64Encoded: string): any { |
|
|
export function objToBase64URI(obj: any): string { |
|
|
const encoded: Uint8Array | number[] = base64js.toByteArray(b64Encoded); |
|
|
return encodeURIComponent(objToBase64(obj)); |
|
|
const json = utf8Decode(encoded); |
|
|
|
|
|
return JSON.parse(json); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function utf8Encode(str: string): Uint8Array | number[] { |
|
|
|
|
|
let result: Uint8Array | number[]; |
|
|
|
|
|
if (isUndefined(Uint8Array)) { |
|
|
|
|
|
result = utf8ToBytes(str); |
|
|
|
|
|
} else { |
|
|
|
|
|
result = new Uint8Array(utf8ToBytes(str)); |
|
|
|
|
|
} |
|
|
|
|
|
return result; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function utf8Decode(bytes: Uint8Array | number[]): string { |
|
|
export function base64toObj(b64Encoded: string): any { |
|
|
return utf8Slice(bytes, 0, bytes.length); |
|
|
const json = decodeURIComponent(atob(b64Encoded).split('').map((c) => { |
|
|
|
|
|
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); |
|
|
|
|
|
}).join('')); |
|
|
|
|
|
return JSON.parse(json); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const scrollRegex = /(auto|scroll)/; |
|
|
const scrollRegex = /(auto|scroll)/; |
|
|
@ -275,129 +268,6 @@ function easeInOut( |
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function utf8Slice(buf: Uint8Array | number[], start: number, end: number): string { |
|
|
|
|
|
let res = ''; |
|
|
|
|
|
let tmp = ''; |
|
|
|
|
|
end = Math.min(buf.length, end || Infinity); |
|
|
|
|
|
start = start || 0; |
|
|
|
|
|
|
|
|
|
|
|
for (let i = start; i < end; i++) { |
|
|
|
|
|
if (buf[i] <= 0x7F) { |
|
|
|
|
|
res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i]); |
|
|
|
|
|
tmp = ''; |
|
|
|
|
|
} else { |
|
|
|
|
|
tmp += '%' + buf[i].toString(16); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return res + decodeUtf8Char(tmp); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function decodeUtf8Char(str: string): string { |
|
|
|
|
|
try { |
|
|
|
|
|
return decodeURIComponent(str); |
|
|
|
|
|
} catch (err) { |
|
|
|
|
|
return String.fromCharCode(0xFFFD); // UTF 8 invalid char
|
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function utf8ToBytes(input: string, units?: number): number[] { |
|
|
|
|
|
units = units || Infinity; |
|
|
|
|
|
let codePoint: number; |
|
|
|
|
|
const length = input.length; |
|
|
|
|
|
let leadSurrogate: number = null; |
|
|
|
|
|
const bytes: number[] = []; |
|
|
|
|
|
let i = 0; |
|
|
|
|
|
|
|
|
|
|
|
for (; i < length; i++) { |
|
|
|
|
|
codePoint = input.charCodeAt(i); |
|
|
|
|
|
|
|
|
|
|
|
// is surrogate component
|
|
|
|
|
|
if (codePoint > 0xD7FF && codePoint < 0xE000) { |
|
|
|
|
|
// last char was a lead
|
|
|
|
|
|
if (leadSurrogate) { |
|
|
|
|
|
// 2 leads in a row
|
|
|
|
|
|
if (codePoint < 0xDC00) { |
|
|
|
|
|
units -= 3; |
|
|
|
|
|
if (units > -1) { bytes.push(0xEF, 0xBF, 0xBD); } |
|
|
|
|
|
leadSurrogate = codePoint; |
|
|
|
|
|
continue; |
|
|
|
|
|
} else { |
|
|
|
|
|
// valid surrogate pair
|
|
|
|
|
|
// tslint:disable-next-line:no-bitwise
|
|
|
|
|
|
codePoint = leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00 | 0x10000; |
|
|
|
|
|
leadSurrogate = null; |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
// no lead yet
|
|
|
|
|
|
|
|
|
|
|
|
if (codePoint > 0xDBFF) { |
|
|
|
|
|
// unexpected trail
|
|
|
|
|
|
units -= 3; |
|
|
|
|
|
if (units > -1) { bytes.push(0xEF, 0xBF, 0xBD); } |
|
|
|
|
|
continue; |
|
|
|
|
|
} else if (i + 1 === length) { |
|
|
|
|
|
// unpaired lead
|
|
|
|
|
|
units -= 3; |
|
|
|
|
|
if (units > -1) { bytes.push(0xEF, 0xBF, 0xBD); } |
|
|
|
|
|
continue; |
|
|
|
|
|
} else { |
|
|
|
|
|
// valid lead
|
|
|
|
|
|
leadSurrogate = codePoint; |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} else if (leadSurrogate) { |
|
|
|
|
|
// valid bmp char, but last char was a lead
|
|
|
|
|
|
units -= 3; |
|
|
|
|
|
if (units > -1) { bytes.push(0xEF, 0xBF, 0xBD); } |
|
|
|
|
|
leadSurrogate = null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// encode utf8
|
|
|
|
|
|
if (codePoint < 0x80) { |
|
|
|
|
|
units -= 1; |
|
|
|
|
|
if (units < 0) { break; } |
|
|
|
|
|
bytes.push(codePoint); |
|
|
|
|
|
} else if (codePoint < 0x800) { |
|
|
|
|
|
units -= 2; |
|
|
|
|
|
if (units < 0) { break; } |
|
|
|
|
|
bytes.push( |
|
|
|
|
|
// tslint:disable-next-line:no-bitwise
|
|
|
|
|
|
codePoint >> 0x6 | 0xC0, |
|
|
|
|
|
// tslint:disable-next-line:no-bitwise
|
|
|
|
|
|
codePoint & 0x3F | 0x80 |
|
|
|
|
|
); |
|
|
|
|
|
} else if (codePoint < 0x10000) { |
|
|
|
|
|
units -= 3; |
|
|
|
|
|
if (units < 0) { break; } |
|
|
|
|
|
bytes.push( |
|
|
|
|
|
// tslint:disable-next-line:no-bitwise
|
|
|
|
|
|
codePoint >> 0xC | 0xE0, |
|
|
|
|
|
// tslint:disable-next-line:no-bitwise
|
|
|
|
|
|
codePoint >> 0x6 & 0x3F | 0x80, |
|
|
|
|
|
// tslint:disable-next-line:no-bitwise
|
|
|
|
|
|
codePoint & 0x3F | 0x80 |
|
|
|
|
|
); |
|
|
|
|
|
} else if (codePoint < 0x200000) { |
|
|
|
|
|
units -= 4; |
|
|
|
|
|
if (units < 0) { break; } |
|
|
|
|
|
bytes.push( |
|
|
|
|
|
// tslint:disable-next-line:no-bitwise
|
|
|
|
|
|
codePoint >> 0x12 | 0xF0, |
|
|
|
|
|
// tslint:disable-next-line:no-bitwise
|
|
|
|
|
|
codePoint >> 0xC & 0x3F | 0x80, |
|
|
|
|
|
// tslint:disable-next-line:no-bitwise
|
|
|
|
|
|
codePoint >> 0x6 & 0x3F | 0x80, |
|
|
|
|
|
// tslint:disable-next-line:no-bitwise
|
|
|
|
|
|
codePoint & 0x3F | 0x80 |
|
|
|
|
|
); |
|
|
|
|
|
} else { |
|
|
|
|
|
throw new Error('Invalid code point'); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return bytes; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function deepClone<T>(target: T, ignoreFields?: string[]): T { |
|
|
export function deepClone<T>(target: T, ignoreFields?: string[]): T { |
|
|
if (target === null) { |
|
|
if (target === null) { |
|
|
return target; |
|
|
return target; |
|
|
|