Add polyfill for padStart function

This commit is contained in:
Riccardo Graziosi
2022-05-07 10:57:27 +02:00
parent 5256ea911a
commit 8195285eb8
2 changed files with 23 additions and 1 deletions

View File

@@ -2,6 +2,8 @@ import * as React from 'react';
import Button from '../../shared/Button';
import padStart from '../../../helpers/padStart';
interface Props {
mode: 'create' | 'update';
@@ -47,7 +49,7 @@ class PostStatusForm extends React.Component<Props, State> {
}
getRandomColor() {
return '#' + (Math.random() * 0xFFFFFF << 0).toString(16).padStart(6, '0');
return '#' + padStart((Math.random() * 0xFFFFFF << 0).toString(16), 6, '0');
}
isFormValid() {

View File

@@ -0,0 +1,20 @@
// padStart has been introduced in ES2017, but right now we're running on ES2016
// This is a MDN polyfill used as an alternative
// TODO: switch to ES2017 and remove this script
function padStart(str, targetLength, padString) {
targetLength = targetLength>>0; //truncate if number or convert non-number to 0;
padString = String((typeof padString !== 'undefined' ? padString : ' '));
if (str.length > targetLength) {
return String(str);
}
else {
targetLength = targetLength-str.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed
}
return padString.slice(0,targetLength) + String(str);
}
};
export default padStart;