diff --git a/.mapping.json b/.mapping.json index 4073a76..a9d4289 100644 --- a/.mapping.json +++ b/.mapping.json @@ -458,6 +458,7 @@ "src/utils/events/types.ts":"metrika/frontend/watch/src/utils/events/types.ts", "src/utils/flagsStorage/__test__/flagsStorage.spec.ts":"metrika/frontend/watch/src/utils/flagsStorage/__test__/flagsStorage.spec.ts", "src/utils/flagsStorage/flagsStorage.ts":"metrika/frontend/watch/src/utils/flagsStorage/flagsStorage.ts", + "src/utils/fletcher/index.ts":"metrika/frontend/watch/src/utils/fletcher/index.ts", "src/utils/fnv32a/__tests__/fnv32a.spec.ts":"metrika/frontend/watch/src/utils/fnv32a/__tests__/fnv32a.spec.ts", "src/utils/fnv32a/fnv32a.ts":"metrika/frontend/watch/src/utils/fnv32a/fnv32a.ts", "src/utils/fnv32a/index.ts":"metrika/frontend/watch/src/utils/fnv32a/index.ts", @@ -541,6 +542,7 @@ "src/utils/phones/phonesDom.ts":"metrika/frontend/watch/src/utils/phones/phonesDom.ts", "src/utils/phones/phonesHide.ts":"metrika/frontend/watch/src/utils/phones/phonesHide.ts", "src/utils/phones/phonesSubscribe.ts":"metrika/frontend/watch/src/utils/phones/phonesSubscribe.ts", + "src/utils/promise/index.ts":"metrika/frontend/watch/src/utils/promise/index.ts", "src/utils/querystring/__tests__/querystring.spec.ts":"metrika/frontend/watch/src/utils/querystring/__tests__/querystring.spec.ts", "src/utils/querystring/index.ts":"metrika/frontend/watch/src/utils/querystring/index.ts", "src/utils/querystring/querystring.ts":"metrika/frontend/watch/src/utils/querystring/querystring.ts", diff --git a/src/utils/fletcher/index.ts b/src/utils/fletcher/index.ts new file mode 100644 index 0000000..7b1b135 --- /dev/null +++ b/src/utils/fletcher/index.ts @@ -0,0 +1,35 @@ +/* eslint-disable no-bitwise */ +/** + * Вычисляет чексумму данных по алгоритму Флетчера. + */ +export function fletcher(data: number[] | string) { + let { length } = data; + let i = 0; + let sum1 = 0xff; + let sum2 = 0xff; + let tlen; + let ch; + let ch2; + while (length) { + tlen = length > 21 ? 21 : length; + length -= tlen; + + do { + ch = typeof data === 'string' ? data.charCodeAt(i) : data[i]; + i += 1; + if (ch > 255) { + ch2 = ch >> 8; + ch &= 0xff; + ch ^= ch2; + } + sum1 += ch; + sum2 += sum1; + // eslint-disable-next-line no-cond-assign + } while ((tlen -= 1)); + sum1 = (sum1 & 0xff) + (sum1 >> 8); + sum2 = (sum2 & 0xff) + (sum2 >> 8); + } + const result = + (((sum1 & 0xff) + (sum1 >> 8)) << 8) | ((sum2 & 0xff) + (sum2 >> 8)); + return result === 0xffff ? 0 : result; +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 22b4ba2..b0a9756 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,74 +1 @@ -/* eslint-disable no-bitwise */ -// https://github.com/RubenVerborgh/promiscuous !?!? -import * as polyPromise from 'promise-polyfill'; -import { POLYFILLS_FEATURE } from 'generated/features'; -import { flags } from '@inject'; -import { toNativeOrFalse } from 'src/utils/function/isNativeFunction/toNativeOrFalse'; -import { includes } from 'src/utils/array/includes'; -import { bind } from './function/bind'; -import { getPath } from './object'; - -/* eslint-disable-next-line import/no-mutable-exports */ -let PolyPromise: PromiseConstructor = window.Promise; - -if (flags[POLYFILLS_FEATURE]) { - const construct = toNativeOrFalse(PolyPromise as any, 'Promise'); - const resolve = toNativeOrFalse( - getPath(PolyPromise, 'resolve')!, - 'resolve', - ); - const reject = toNativeOrFalse(getPath(PolyPromise, 'reject')!, 'reject'); - const all = toNativeOrFalse(getPath(PolyPromise, 'all')!, 'all'); - if (includes(false, [construct, resolve, reject, all])) { - PolyPromise = polyPromise.default; - } else { - const anyPromise = function promiseWrapper(a: any) { - return new Promise(a); - } as any; - anyPromise.resolve = bind(resolve as any, PolyPromise); - anyPromise.reject = bind(reject as any, PolyPromise); - anyPromise.all = bind(all as any, PolyPromise); - PolyPromise = anyPromise; - } -} - -export { PolyPromise }; - -/** - * Вычисляет чексумму данных по алгоритму Флетчера. - * - * @param {Array|String} data - * - * @returns {Number} - */ -export function fletcher(data: number[] | string) { - let { length } = data; - let i = 0; - let sum1 = 0xff; - let sum2 = 0xff; - let tlen; - let ch; - let ch2; - while (length) { - tlen = length > 21 ? 21 : length; - length -= tlen; - - do { - ch = typeof data === 'string' ? data.charCodeAt(i) : data[i]; - i += 1; - if (ch > 255) { - ch2 = ch >> 8; - ch &= 0xff; - ch ^= ch2; - } - sum1 += ch; - sum2 += sum1; - // eslint-disable-next-line no-cond-assign - } while ((tlen -= 1)); - sum1 = (sum1 & 0xff) + (sum1 >> 8); - sum2 = (sum2 & 0xff) + (sum2 >> 8); - } - const result = - (((sum1 & 0xff) + (sum1 >> 8)) << 8) | ((sum2 & 0xff) + (sum2 >> 8)); - return result === 0xffff ? 0 : result; -} +export * from './promise'; diff --git a/src/utils/promise/index.ts b/src/utils/promise/index.ts new file mode 100644 index 0000000..25e7f78 --- /dev/null +++ b/src/utils/promise/index.ts @@ -0,0 +1,34 @@ +/* eslint-disable no-bitwise */ +// https://github.com/RubenVerborgh/promiscuous !?!? +import * as polyPromise from 'promise-polyfill'; +import { POLYFILLS_FEATURE } from 'generated/features'; +import { flags } from '@inject'; +import { bind } from 'src/utils/function/bind'; +import { toNativeOrFalse } from 'src/utils/function/isNativeFunction/toNativeOrFalse'; +import { getPath } from 'src/utils/object'; + +/* eslint-disable-next-line import/no-mutable-exports */ +export let PolyPromise: PromiseConstructor = window.Promise; + +if (flags[POLYFILLS_FEATURE]) { + const construct = toNativeOrFalse(PolyPromise as any, 'Promise'); + const resolve = toNativeOrFalse( + getPath(PolyPromise, 'resolve')!, + 'resolve', + ); + const reject = toNativeOrFalse(getPath(PolyPromise, 'reject')!, 'reject'); + const all = toNativeOrFalse(getPath(PolyPromise, 'all')!, 'all'); + if (construct && resolve && reject && all) { + const anyPromise = function promiseWrapper( + a: ConstructorParameters[0], + ) { + return new Promise(a); + } as any; + anyPromise.resolve = bind(resolve, PolyPromise); + anyPromise.reject = bind(reject, PolyPromise); + anyPromise.all = bind(all, PolyPromise); + PolyPromise = anyPromise; + } else { + PolyPromise = polyPromise.default; + } +}