Skip to content

Commit

Permalink
Clean up utils index file
Browse files Browse the repository at this point in the history
Clean up utils index file
  • Loading branch information
sabio committed Sep 26, 2023
1 parent 364f3cb commit baff9e9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 74 deletions.
2 changes: 2 additions & 0 deletions .mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
35 changes: 35 additions & 0 deletions src/utils/fletcher/index.ts
Original file line number Diff line number Diff line change
@@ -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;
}
75 changes: 1 addition & 74 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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';
34 changes: 34 additions & 0 deletions src/utils/promise/index.ts
Original file line number Diff line number Diff line change
@@ -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<PromiseConstructor>[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;
}
}

0 comments on commit baff9e9

Please sign in to comment.