chore: replace deprecated String.prototype.substr() (#6333)
* chore: replace deprecated String.prototype.substr() .substr() is deprecated so we replace it with .slice() which works similarily but isn't deprecated Signed-off-by: Tobias Speicher <rootcommander@gmail.com> * refactor: add prefer slice lint rule and fix errors Co-authored-by: erezrokah <erezrokah@users.noreply.github.com>
This commit is contained in:
@ -292,7 +292,7 @@ function prepareMetaPath(path: string, collection: Collection) {
|
||||
return path;
|
||||
}
|
||||
const dir = dirname(path);
|
||||
return dir.substr(collection.get('folder')!.length + 1) || '/';
|
||||
return dir.slice(collection.get('folder')!.length + 1) || '/';
|
||||
}
|
||||
|
||||
function collectionDepth(collection: Collection) {
|
||||
|
@ -117,7 +117,7 @@ export class EntriesCollection extends React.Component {
|
||||
|
||||
export function filterNestedEntries(path, collectionFolder, entries) {
|
||||
const filtered = entries.filter(e => {
|
||||
const entryPath = e.get('path').substring(collectionFolder.length + 1);
|
||||
const entryPath = e.get('path').slice(collectionFolder.length + 1);
|
||||
if (!entryPath.startsWith(path)) {
|
||||
return false;
|
||||
}
|
||||
@ -125,7 +125,7 @@ export function filterNestedEntries(path, collectionFolder, entries) {
|
||||
// only show immediate children
|
||||
if (path) {
|
||||
// non root path
|
||||
const trimmed = entryPath.substring(path.length + 1);
|
||||
const trimmed = entryPath.slice(path.length + 1);
|
||||
return trimmed.split('/').length === 2;
|
||||
} else {
|
||||
// root path
|
||||
|
@ -144,7 +144,7 @@ export function getTreeData(collection, entries) {
|
||||
const rootFolder = '/';
|
||||
const entriesObj = entries
|
||||
.toJS()
|
||||
.map(e => ({ ...e, path: e.path.substring(collectionFolder.length) }));
|
||||
.map(e => ({ ...e, path: e.path.slice(collectionFolder.length) }));
|
||||
|
||||
const dirs = entriesObj.reduce((acc, entry) => {
|
||||
let dir = dirname(entry.path);
|
||||
|
@ -27,7 +27,7 @@ const parsers = {
|
||||
parse: (input: string) => {
|
||||
let JSONinput = input.trim();
|
||||
// Fix JSON if leading and trailing brackets were trimmed.
|
||||
if (JSONinput.substr(0, 1) !== '{') {
|
||||
if (JSONinput.slice(0, 1) !== '{') {
|
||||
JSONinput = '{' + JSONinput + '}';
|
||||
}
|
||||
return jsonFormatter.fromFile(JSONinput);
|
||||
@ -35,7 +35,8 @@ const parsers = {
|
||||
stringify: (metadata: object) => {
|
||||
let JSONoutput = jsonFormatter.toFile(metadata).trim();
|
||||
// Trim leading and trailing brackets.
|
||||
if (JSONoutput.substr(0, 1) === '{' && JSONoutput.substr(-1) === '}') {
|
||||
if (JSONoutput.slice(0, 1) === '{' && JSONoutput.slice(-1) === '}') {
|
||||
// eslint-disable-next-line unicorn/prefer-string-slice
|
||||
JSONoutput = JSONoutput.substring(1, JSONoutput.length - 1);
|
||||
}
|
||||
return JSONoutput;
|
||||
@ -54,8 +55,9 @@ const parsers = {
|
||||
};
|
||||
|
||||
function inferFrontmatterFormat(str: string) {
|
||||
const firstLine = str.substr(0, str.indexOf('\n')).trim();
|
||||
if (firstLine.length > 3 && firstLine.substr(0, 3) === '---') {
|
||||
// eslint-disable-next-line unicorn/prefer-string-slice
|
||||
const firstLine = str.substring(0, str.indexOf('\n')).trim();
|
||||
if (firstLine.length > 3 && firstLine.slice(0, 3) === '---') {
|
||||
// No need to infer, `gray-matter` will handle things like `---toml` for us.
|
||||
return;
|
||||
}
|
||||
@ -130,6 +132,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;
|
||||
}
|
||||
}
|
||||
|
@ -7,5 +7,6 @@ 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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user