Skip to content

Commit

Permalink
chore: remove all .substring() usage (decaporg#6352)
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Speicher <[email protected]>
  • Loading branch information
CommanderRoot authored Mar 30, 2022
1 parent 59b9348 commit ad82a0f
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cypress/plugins/bitbucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async function prepareTestBitBucketRepo({ lfs }) {
// postfix a random string to avoid collisions
const postfix = Math.random()
.toString(32)
.substring(2);
.slice(2);
const testRepoName = `${repo}-${Date.now()}-${postfix}`;

console.log('Creating repository', testRepoName);
Expand Down
2 changes: 1 addition & 1 deletion cypress/plugins/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async function prepareTestGitHubRepo() {
// postfix a random string to avoid collisions
const postfix = Math.random()
.toString(32)
.substring(2);
.slice(2);
const testRepoName = `${repo}-${Date.now()}-${postfix}`;

const client = getGitHubClient(token);
Expand Down
2 changes: 1 addition & 1 deletion cypress/plugins/gitlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async function prepareTestGitLabRepo() {
// postfix a random string to avoid collisions
const postfix = Math.random()
.toString(32)
.substring(2);
.slice(2);
const testRepoName = `${repo}-${Date.now()}-${postfix}`;

const client = getGitLabClient(token);
Expand Down
2 changes: 1 addition & 1 deletion cypress/plugins/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ let serverProcess;
async function setupProxy(options) {
const postfix = Math.random()
.toString(32)
.substring(2);
.slice(2);

const testRepoName = `proxy-test-repo-${Date.now()}-${postfix}`;
const tempDir = path.join('.temp', testRepoName);
Expand Down
10 changes: 4 additions & 6 deletions packages/netlify-cms-core/src/formats/frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ const parsers = {
let JSONoutput = jsonFormatter.toFile(metadata).trim();
// Trim leading and trailing brackets.
if (JSONoutput.slice(0, 1) === '{' && JSONoutput.slice(-1) === '}') {
// eslint-disable-next-line unicorn/prefer-string-slice
JSONoutput = JSONoutput.substring(1, JSONoutput.length - 1);
JSONoutput = JSONoutput.slice(1, -1);
}
return JSONoutput;
},
Expand All @@ -55,8 +54,8 @@ const parsers = {
};

function inferFrontmatterFormat(str: string) {
// eslint-disable-next-line unicorn/prefer-string-slice
const firstLine = str.substring(0, str.indexOf('\n')).trim();
const lineEnd = str.indexOf('\n');
const firstLine = str.slice(0, lineEnd !== -1 ? lineEnd : 0).trim();
if (firstLine.length > 3 && firstLine.slice(0, 3) === '---') {
// No need to infer, `gray-matter` will handle things like `---toml` for us.
return;
Expand Down Expand Up @@ -132,8 +131,7 @@ export class FrontmatterFormatter {
comments,
...format,
});
// eslint-disable-next-line unicorn/prefer-string-slice
return trimLastLineBreak && file.slice(-1) === '\n' ? file.substring(0, file.length - 1) : file;
return trimLastLineBreak && file.slice(-1) === '\n' ? file.slice(0, -1) : file;
}
}

Expand Down
3 changes: 1 addition & 2 deletions packages/netlify-cms-core/src/lib/textHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ export function stringToRGB(str) {

const c = (hash & 0x00ffffff).toString(16).toUpperCase();

// eslint-disable-next-line unicorn/prefer-string-slice
return '00000'.substring(0, 6 - c.length) + c;
return `00000${c}`.slice(-6);
}
6 changes: 1 addition & 5 deletions packages/netlify-cms-widget-file/src/withFileControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,7 @@ export default function withFileControl({ forImage } = {}) {
if (!value || value.length <= size) {
return value;
}
// eslint-disable-next-line unicorn/prefer-string-slice
const text = `${value.substring(0, size / 2)}\u2026${value.substring(
value.length - size / 2 + 1,
value.length,
)}`;
const text = `${value.slice(0, size / 2)}\u2026${value.slice(-(size / 2) + 1)}`;
return (
<FileLink href={value} rel="noopener" target="_blank">
{text}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,8 @@ export default function slateToRemark(raw, { voidCodeBlock }) {
const index = node.text.search(exp);
if (index > -1) {
const substringIndex = trailing ? index : index + 1;
// eslint-disable-next-line unicorn/prefer-string-slice
const firstSplit = node.text.substring(0, substringIndex);
// eslint-disable-next-line unicorn/prefer-string-slice
const secondSplit = node.text.substring(substringIndex);
const firstSplit = node.text.slice(0, substringIndex);
const secondSplit = node.text.slice(substringIndex);
const whitespace = trailing ? secondSplit : firstSplit;
const text = trailing ? firstSplit : secondSplit;
return { whitespace, trimmedNode: { ...node, text } };
Expand Down

0 comments on commit ad82a0f

Please sign in to comment.