From f29cff110b20a67e7964d13013431a3d3b70ff37 Mon Sep 17 00:00:00 2001 From: Dhruv Bhanushali Date: Mon, 28 Feb 2022 18:18:12 +0400 Subject: [PATCH 01/13] Convert the `resampling` util to TypeScript --- src/utils/{resampling.js => resampling.ts} | 52 +++++++++++----------- 1 file changed, 27 insertions(+), 25 deletions(-) rename src/utils/{resampling.js => resampling.ts} (62%) diff --git a/src/utils/resampling.js b/src/utils/resampling.ts similarity index 62% rename from src/utils/resampling.js rename to src/utils/resampling.ts index c0ca4e9f42..2ae052c45a 100644 --- a/src/utils/resampling.js +++ b/src/utils/resampling.ts @@ -2,23 +2,22 @@ * Resizes a given array to consist of more elements than provided. This uses * linear interpolation to fill in the gaps. * - * @param data {number[]} the list of data points to interpolate - * @param threshold {number} the number of expected data points from the array - * @returns {number[]} the array with the required number of points + * @param data the list of data points to interpolate + * @param threshold the number of expected data points from the array + * @returns the array with the required number of points */ -export let upsampleArray = (data, threshold) => { - let linearInterpolate = function (before, after, atPoint) { - return before + (after - before) * atPoint - } +export const upsampleArray = (data: number[], threshold: number): number[] => { + const linearInterpolate = (before: number, after: number, atPoint: number) => + before + (after - before) * atPoint - let newData = [] - let springFactor = (data.length - 1) / (threshold - 1) + const newData = [] + const springFactor = (data.length - 1) / (threshold - 1) newData[0] = data[0] // for new allocation for (let i = 1; i < threshold - 1; i++) { - let tmp = i * springFactor - let before = Math.floor(tmp).toFixed() - let after = Math.ceil(tmp).toFixed() - let atPoint = tmp - before + const tmp = i * springFactor + const before = Math.floor(tmp) + const after = Math.ceil(tmp) + const atPoint = tmp - before newData[i] = linearInterpolate(data[before], data[after], atPoint) } newData[threshold - 1] = data[data.length - 1] // for new allocation @@ -26,23 +25,23 @@ export let upsampleArray = (data, threshold) => { } /** - * Resizes a given array to consist of less elements than provided. This uses + * Resizes a given array to consist of fewer elements than provided. This uses * the Largest Triangle Three Buckets algorithm by Sveinn Steinarsson. * * @see {@link https://github.com/sveinn-steinarsson/flot-downsample} * - * @param data {number[]} the list of data points to interpolate - * @param threshold {number} the number of expected data points from the array - * @returns {number[]} the array with the required number of points + * @param data - the list of data points to interpolate + * @param threshold - the number of expected data points from the array + * @returns the array with the required number of points */ -export const downsampleArray = (data, threshold) => { - let dataLength = data.length +export const downsampleArray = (data: number[], threshold: number) => { + const dataLength = data.length - let sampled = [] + const sampled = [] let sampled_index = 0 // Bucket size, except first and last point - let every = (dataLength - 2) / (threshold - 2) + const every = (dataLength - 2) / (threshold - 2) let a = 0 let max_area_point, max_area, area, next_a @@ -56,7 +55,7 @@ export const downsampleArray = (data, threshold) => { let avg_range_end = Math.floor((i + 2) * every) + 1 avg_range_end = avg_range_end < dataLength ? avg_range_end : dataLength - let avg_range_length = avg_range_end - avg_range_start + const avg_range_length = avg_range_end - avg_range_start for (; avg_range_start < avg_range_end; avg_range_start++) { avg_x += avg_range_start @@ -67,13 +66,16 @@ export const downsampleArray = (data, threshold) => { // Get the range for this bucket let range_offs = Math.floor(i * every) + 1 - let range_to = Math.floor((i + 1) * every) + 1 + const range_to = Math.floor((i + 1) * every) + 1 - let point_a_x = a - let point_a_y = data[a] + const point_a_x = a + const point_a_y = data[a] max_area = area = -1 + max_area_point = 0 + next_a = 0 + for (; range_offs < range_to; range_offs++) { // Calculate triangle area over three buckets area = From be13af55ea5fcb8d285d2982a0db1b4dc7d7c409 Mon Sep 17 00:00:00 2001 From: Dhruv Bhanushali Date: Mon, 28 Feb 2022 18:42:41 +0400 Subject: [PATCH 02/13] Convert the `decode-data` util to TypeScript --- src/utils/decode-data.js | 23 ----------------------- src/utils/decode-data.ts | 22 ++++++++++++++++++++++ src/utils/decode-media-data.js | 2 +- test/unit/specs/utils/decode-data.spec.js | 4 ++-- tsconfig.json | 1 - 5 files changed, 25 insertions(+), 27 deletions(-) delete mode 100644 src/utils/decode-data.js create mode 100644 src/utils/decode-data.ts diff --git a/src/utils/decode-data.js b/src/utils/decode-data.js deleted file mode 100644 index 5f36e45354..0000000000 --- a/src/utils/decode-data.js +++ /dev/null @@ -1,23 +0,0 @@ -/** - * decodes some edge cases of Unicode characters with an extra \ - * See test cases for some examples - * @param {string} [data] - */ -export default function decodeData(data) { - if (data) { - try { - const regexASCII = /\\x([\d\w]{2})/gi - const ascii = data.replace(regexASCII, (_, grp) => - String.fromCharCode(parseInt(grp, 16)) - ) - const regexUni = /\\u([\d\w]{4})/gi - const uni = ascii.replace(regexUni, (_, grp) => - String.fromCharCode(parseInt(grp, 16)) - ) - return decodeURI(uni) - } catch (e) { - return data - } - } - return '' -} diff --git a/src/utils/decode-data.ts b/src/utils/decode-data.ts new file mode 100644 index 0000000000..121c6a8ea1 --- /dev/null +++ b/src/utils/decode-data.ts @@ -0,0 +1,22 @@ +/** + * Decodes some edge cases where ASCII/Unicode characters with escape sequences + * have escaped backslashes, which prevents them from rendering. This function + * replaces them with the relevant character. + * + * @param data - the string to unescape so that it can be rendered + */ +export const decodeData = (data = ''): string => { + if (!data) return '' + + try { + const regexes = [/\\x([\d\w]{2})/gi, /\\u([\d\w]{4})/gi] + regexes.forEach((regex) => { + data = data.replace(regex, (_, grp) => + String.fromCharCode(parseInt(grp, 16)) + ) + }) + return decodeURI(data) + } catch (e) { + return data + } +} diff --git a/src/utils/decode-media-data.js b/src/utils/decode-media-data.js index 70f7c16255..cfbc0cd7cc 100644 --- a/src/utils/decode-media-data.js +++ b/src/utils/decode-media-data.js @@ -1,4 +1,4 @@ -import decodeData from '~/utils/decode-data' +import { decodeData } from '~/utils/decode-data' import { IMAGE } from '~/constants/media' /** diff --git a/test/unit/specs/utils/decode-data.spec.js b/test/unit/specs/utils/decode-data.spec.js index 7a5b4ed737..4de26cc8eb 100644 --- a/test/unit/specs/utils/decode-data.spec.js +++ b/test/unit/specs/utils/decode-data.spec.js @@ -1,4 +1,4 @@ -import decodeData from '~/utils/decode-data' +import { decodeData } from '~/utils/decode-data' describe('decodeData', () => { it('returns empty string for empty string', () => { @@ -16,7 +16,7 @@ describe('decodeData', () => { it('returns decoded ASCII hexacode strings', () => { const data = 's\\xe9' - expect(decodeData(data)).toBe('s\xe9') + expect(decodeData(data)).toBe('sé') }) it('returns decoded unicode strings', () => { diff --git a/tsconfig.json b/tsconfig.json index d4457abc28..48b434f95b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -46,7 +46,6 @@ "src/data/api-service.js", "src/data/usage-data-service.js", "src/store/user.js", - "src/utils/decode-data.js", "src/utils/decode-media-data.js", "src/utils/deep-freeze.js", "src/utils/key-codes.js", From bb215be1bf8c9068b61a0b4059bc16be5a8cdd88 Mon Sep 17 00:00:00 2001 From: Dhruv Bhanushali Date: Mon, 28 Feb 2022 19:06:58 +0400 Subject: [PATCH 03/13] Convert the `warn` util to TypeScript and update usages --- src/components/SketchFabViewer.vue | 5 +++-- src/components/VPopover/meta/VPopover.stories.js | 6 ++++-- src/components/meta/DropdownButton.stories.js | 6 ++++-- src/store/index.js | 4 +++- src/store/provider.js | 3 ++- src/utils/local.js | 5 +++-- src/utils/warn.js | 6 ------ src/utils/warn.ts | 9 +++++++++ tsconfig.json | 3 +-- 9 files changed, 29 insertions(+), 18 deletions(-) delete mode 100644 src/utils/warn.js create mode 100644 src/utils/warn.ts diff --git a/src/components/SketchFabViewer.vue b/src/components/SketchFabViewer.vue index 38ef9d440e..6de9eac15a 100644 --- a/src/components/SketchFabViewer.vue +++ b/src/components/SketchFabViewer.vue @@ -16,6 +16,7 @@