Replace deprecated substr with slice (#13822)

This commit is contained in:
Andreas Maechler 2023-03-15 04:57:06 -06:00 committed by GitHub
parent ed57c5c853
commit 46766d245c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 23 additions and 21 deletions

View File

@ -44,7 +44,7 @@ fs.ensureDirSync(path);
console.log('Making path:', path); console.log('Making path:', path);
const camelName = name.replace(/(^|-)[a-z]/g, s => s.replace('-', '').toUpperCase()); const camelName = name.replace(/(^|-)[a-z]/g, s => s.replace('-', '').toUpperCase());
const snakeName = camelName[0].toLowerCase() + camelName.substr(1); const snakeName = camelName[0].toLowerCase() + camelName.slice(1);
function writeFile(path, data) { function writeFile(path, data) {
try { try {

View File

@ -39,8 +39,8 @@ function shortenString(str: string): ShortParts {
// Print something like: // Print something like:
// BAAAArAAEiQKpDAEAACwZCBAGSBgiSEAAAAQpAIDwAg...23 omitted...gwiRoQBJIC // BAAAArAAEiQKpDAEAACwZCBAGSBgiSEAAAAQpAIDwAg...23 omitted...gwiRoQBJIC
const omit = str.length - (MAX_CHARS_TO_SHOW - 17); const omit = str.length - (MAX_CHARS_TO_SHOW - 17);
const prefix = str.substr(0, str.length - (omit + 10)); const prefix = str.slice(0, str.length - (omit + 10));
const suffix = str.substr(str.length - 10); const suffix = str.slice(str.length - 10);
return { return {
prefix, prefix,
omitted: `...${omit} omitted...`, omitted: `...${omit} omitted...`,
@ -73,7 +73,7 @@ export const TableCell = React.memo(function TableCell(props: TableCellProps) {
<div className="table-cell plain"> <div className="table-cell plain">
{str.length < ABSOLUTE_MAX_CHARS_TO_SHOW {str.length < ABSOLUTE_MAX_CHARS_TO_SHOW
? str ? str
: `${str.substr(0, ABSOLUTE_MAX_CHARS_TO_SHOW)}...`} : `${str.slice(0, ABSOLUTE_MAX_CHARS_TO_SHOW)}...`}
</div> </div>
); );
} }

View File

@ -195,7 +195,7 @@ export class WorkbenchQuery {
.map(line => .map(line =>
line.replace( line.replace(
/^(\s*)(INSERT\s+INTO|REPLACE\s+INTO|OVERWRITE|PARTITIONED\s+BY|CLUSTERED\s+BY)/i, /^(\s*)(INSERT\s+INTO|REPLACE\s+INTO|OVERWRITE|PARTITIONED\s+BY|CLUSTERED\s+BY)/i,
(_, spaces, thing) => `${spaces}--${thing.substr(2)}`, (_, spaces, thing) => `${spaces}--${thing.slice(2)}`,
), ),
) )
.join('\n'); .join('\n');

View File

@ -21,7 +21,7 @@ import type { DateRange } from '@blueprintjs/datetime';
const CURRENT_YEAR = new Date().getUTCFullYear(); const CURRENT_YEAR = new Date().getUTCFullYear();
export function dateToIsoDateString(date: Date): string { export function dateToIsoDateString(date: Date): string {
return date.toISOString().substr(0, 10); return date.toISOString().slice(0, 10);
} }
export function utcToLocalDate(utcDate: Date): Date { export function utcToLocalDate(utcDate: Date): Date {
@ -52,8 +52,8 @@ export function localDateRangeToInterval(localRange: DateRange): string {
// This function takes in the dates selected from datepicker in local time, and displays them in UTC // This function takes in the dates selected from datepicker in local time, and displays them in UTC
// Shall Blueprint make any changes to the way dates are selected, this function will have to be reworked // Shall Blueprint make any changes to the way dates are selected, this function will have to be reworked
const [localStartDate, localEndDate] = localRange; const [localStartDate, localEndDate] = localRange;
return `${localStartDate ? localToUtcDate(localStartDate).toISOString().substring(0, 19) : ''}/${ return `${localStartDate ? localToUtcDate(localStartDate).toISOString().slice(0, 19) : ''}/${
localEndDate ? localToUtcDate(localEndDate).toISOString().substring(0, 19) : '' localEndDate ? localToUtcDate(localEndDate).toISOString().slice(0, 19) : ''
}`; }`;
} }

View File

@ -20,7 +20,7 @@ const UNSAFE_CHAR = /[^a-z0-9 ,._\-;:(){}[\]<>!@#$%^&*`~?]/gi;
function escape(str: string): string { function escape(str: string): string {
return str.replace(UNSAFE_CHAR, s => { return str.replace(UNSAFE_CHAR, s => {
return '\\u' + ('000' + s.charCodeAt(0).toString(16)).substr(-4); return '\\u' + ('000' + s.charCodeAt(0).toString(16)).slice(-4);
}); });
} }

View File

@ -32,13 +32,15 @@ const JSON_ESCAPES: Record<string, string> = {
't': '\t', 't': '\t',
}; };
// The stringifier is just JSON minus the double quotes, the parser is much more forgiving /**
* The stringifier is just JSON minus the double quotes, the parser is much more forgiving.
*/
export const JSON_STRING_FORMATTER: Formatter<string> = { export const JSON_STRING_FORMATTER: Formatter<string> = {
stringify: (str: string) => { stringify: (str: string) => {
if (typeof str !== 'string') return ''; if (typeof str !== 'string') return '';
const json = JSON.stringify(str); const json = JSON.stringify(str);
return json.substr(1, json.length - 2); return json.slice(1, json.length - 1);
}, },
parse: (str: string) => { parse: (str: string) => {
const n = str.length; const n = str.length;
@ -48,8 +50,8 @@ export const JSON_STRING_FORMATTER: Formatter<string> = {
const ch = str[i]; const ch = str[i];
if (ch === '\\') { if (ch === '\\') {
const nextCh = str[i + 1]; const nextCh = str[i + 1];
if (nextCh === 'u' && /^[0-9a-f]{4}$/i.test(str.substr(i + 2, 4))) { if (nextCh === 'u' && /^[0-9a-f]{4}$/i.test(str.slice(i + 2, i + 6))) {
parsed += String.fromCharCode(parseInt(str.substr(i + 2, 4), 16)); parsed += String.fromCharCode(parseInt(str.slice(i + 2, i + 6), 16));
i += 6; i += 6;
} else if (JSON_ESCAPES[nextCh]) { } else if (JSON_ESCAPES[nextCh]) {
parsed += JSON_ESCAPES[nextCh]; parsed += JSON_ESCAPES[nextCh];

View File

@ -218,11 +218,11 @@ export function formatMillions(n: NumberLike): string {
} }
function pad2(str: string | number): string { function pad2(str: string | number): string {
return ('00' + str).substr(-2); return ('00' + str).slice(-2);
} }
function pad3(str: string | number): string { function pad3(str: string | number): string {
return ('000' + str).substr(-3); return ('000' + str).slice(-3);
} }
export function formatDuration(ms: NumberLike): string { export function formatDuration(ms: NumberLike): string {
@ -252,7 +252,7 @@ export function formatDurationHybrid(ms: NumberLike): string {
const timeInMs = Math.floor(n) % 1000; const timeInMs = Math.floor(n) % 1000;
return `${timeInMin ? `${timeInMin}:` : ''}${timeInMin ? pad2(timeInSec) : timeInSec}.${pad3( return `${timeInMin ? `${timeInMin}:` : ''}${timeInMin ? pad2(timeInSec) : timeInSec}.${pad3(
timeInMs, timeInMs,
).substring(0, 2)}s`; ).slice(0, 2)}s`;
} else { } else {
return formatDuration(n); return formatDuration(n);
} }
@ -413,7 +413,7 @@ export function parseCsvLine(line: string): string[] {
let m: RegExpExecArray | null; let m: RegExpExecArray | null;
while ((m = /^,(?:"([^"]*(?:""[^"]*)*)"|([^,\r\n]*))/m.exec(line))) { while ((m = /^,(?:"([^"]*(?:""[^"]*)*)"|([^,\r\n]*))/m.exec(line))) {
parts.push(typeof m[1] === 'string' ? m[1].replace(/""/g, '"') : m[2]); parts.push(typeof m[1] === 'string' ? m[1].replace(/""/g, '"') : m[2]);
line = line.substr(m[0].length); line = line.slice(m[0].length);
} }
return parts; return parts;
} }
@ -456,5 +456,5 @@ export function tickIcon(checked: boolean): IconName {
} }
export function generate8HexId(): string { export function generate8HexId(): string {
return (Math.random() * 1e10).toString(16).replace('.', '').substr(0, 8); return (Math.random() * 1e10).toString(16).replace('.', '').slice(0, 8);
} }

View File

@ -50,7 +50,7 @@ export function findEmptyLiteralPosition(query: SqlQuery): RowColumn | undefined
const crazyIndex = subQueryString.indexOf(CRAZY_STRING); const crazyIndex = subQueryString.indexOf(CRAZY_STRING);
if (crazyIndex < 0) return; if (crazyIndex < 0) return;
const prefix = subQueryString.substr(0, crazyIndex); const prefix = subQueryString.slice(0, crazyIndex);
const lines = prefix.split(/\n/g); const lines = prefix.split(/\n/g);
const row = lines.length - 1; const row = lines.length - 1;
const lastLine = lines[row]; const lastLine = lines[row];

View File

@ -207,7 +207,7 @@ function showRawLine(line: SampleEntry): string {
return `[Multi-line row, length: ${raw.length}]`; return `[Multi-line row, length: ${raw.length}]`;
} }
if (raw.length > 1000) { if (raw.length > 1000) {
return raw.substr(0, 1000) + '...'; return raw.slice(0, 1000) + '...';
} }
return raw; return raw;
} }
@ -1220,7 +1220,7 @@ export class LoadDataView extends React.PureComponent<LoadDataViewProps, LoadDat
placeholder="Paste your data here" placeholder="Paste your data here"
value={deepGet(spec, 'spec.ioConfig.inputSource.data')} value={deepGet(spec, 'spec.ioConfig.inputSource.data')}
onChange={(e: any) => { onChange={(e: any) => {
const stringValue = e.target.value.substr(0, MAX_INLINE_DATA_LENGTH); const stringValue = e.target.value.slice(0, MAX_INLINE_DATA_LENGTH);
this.updateSpecPreview(deepSet(spec, 'spec.ioConfig.inputSource.data', stringValue)); this.updateSpecPreview(deepSet(spec, 'spec.ioConfig.inputSource.data', stringValue));
}} }}
/> />