signal-desktop/ts/components/_util.ts

31 lines
878 B
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2019-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2020-09-12 00:46:52 +00:00
import { MutableRefObject, Ref } from 'react';
2020-01-17 22:23:19 +00:00
import { isFunction } from 'lodash';
2020-03-23 21:09:12 +00:00
import memoizee from 'memoizee';
2020-01-17 22:23:19 +00:00
2019-11-19 23:03:00 +00:00
export function cleanId(id: string): string {
return id.replace(/[^\u0020-\u007e\u00a0-\u00ff]/g, '_');
}
2020-01-17 22:23:19 +00:00
2020-09-12 00:46:52 +00:00
// Memoizee makes this difficult.
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
2020-03-23 21:09:12 +00:00
export const createRefMerger = () =>
memoizee(
<T>(...refs: Array<Ref<T>>) => {
return (t: T) => {
refs.forEach(r => {
if (isFunction(r)) {
r(t);
} else if (r) {
2020-09-12 00:46:52 +00:00
// Using a MutableRefObject as intended
// eslint-disable-next-line no-param-reassign
(r as MutableRefObject<T>).current = t;
2020-03-23 21:09:12 +00:00
}
});
};
},
{ length: false, max: 1 }
);