mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
refactor(csv): rename graphemeLength
to codePointLength
(#5421)
This commit is contained in:
parent
1b53ea72eb
commit
c6af0c6e80
10
csv/_io.ts
10
csv/_io.ts
@ -4,7 +4,7 @@
|
|||||||
// https://github.com/golang/go/blob/master/LICENSE
|
// https://github.com/golang/go/blob/master/LICENSE
|
||||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
import { graphemeLength } from "./_shared.ts";
|
import { codePointLength } from "./_shared.ts";
|
||||||
|
|
||||||
/** Options for {@linkcode parseRecord}. */
|
/** Options for {@linkcode parseRecord}. */
|
||||||
export interface ReadOptions {
|
export interface ReadOptions {
|
||||||
@ -98,7 +98,7 @@ export async function parseRecord(
|
|||||||
if (!options.lazyQuotes) {
|
if (!options.lazyQuotes) {
|
||||||
const j = field.indexOf(quote);
|
const j = field.indexOf(quote);
|
||||||
if (j >= 0) {
|
if (j >= 0) {
|
||||||
const col = graphemeLength(
|
const col = codePointLength(
|
||||||
fullLine.slice(0, fullLine.length - line.slice(j).length),
|
fullLine.slice(0, fullLine.length - line.slice(j).length),
|
||||||
);
|
);
|
||||||
throw new ParseError(startLine + 1, lineIndex, col, ERR_BARE_QUOTE);
|
throw new ParseError(startLine + 1, lineIndex, col, ERR_BARE_QUOTE);
|
||||||
@ -138,7 +138,7 @@ export async function parseRecord(
|
|||||||
recordBuffer += quote;
|
recordBuffer += quote;
|
||||||
} else {
|
} else {
|
||||||
// `"*` sequence (invalid non-escaped quote).
|
// `"*` sequence (invalid non-escaped quote).
|
||||||
const col = graphemeLength(
|
const col = codePointLength(
|
||||||
fullLine.slice(0, fullLine.length - line.length - quoteLen),
|
fullLine.slice(0, fullLine.length - line.length - quoteLen),
|
||||||
);
|
);
|
||||||
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
|
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
|
||||||
@ -153,7 +153,7 @@ export async function parseRecord(
|
|||||||
if (r === null) {
|
if (r === null) {
|
||||||
// Abrupt end of file (EOF or error).
|
// Abrupt end of file (EOF or error).
|
||||||
if (!options.lazyQuotes) {
|
if (!options.lazyQuotes) {
|
||||||
const col = graphemeLength(fullLine);
|
const col = codePointLength(fullLine);
|
||||||
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
|
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
|
||||||
}
|
}
|
||||||
fieldIndexes.push(recordBuffer.length);
|
fieldIndexes.push(recordBuffer.length);
|
||||||
@ -163,7 +163,7 @@ export async function parseRecord(
|
|||||||
} else {
|
} else {
|
||||||
// Abrupt end of file (EOF on error).
|
// Abrupt end of file (EOF on error).
|
||||||
if (!options.lazyQuotes) {
|
if (!options.lazyQuotes) {
|
||||||
const col = graphemeLength(fullLine);
|
const col = codePointLength(fullLine);
|
||||||
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
|
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
|
||||||
}
|
}
|
||||||
fieldIndexes.push(recordBuffer.length);
|
fieldIndexes.push(recordBuffer.length);
|
||||||
|
@ -1,15 +1,13 @@
|
|||||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns length of a string considering surrogate pairs
|
* Gives the length of a string in Unicode code points
|
||||||
* ```ts
|
*
|
||||||
* function graphemeLength(s: string) {
|
* ```
|
||||||
* return Array.from(s).length;
|
* codePointLength("🐱"); // 1
|
||||||
* }
|
* "🐱".length; // 2
|
||||||
* graphemeLength("🐱") // 1
|
|
||||||
* "🐱".length // 2
|
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export function graphemeLength(s: string) {
|
export function codePointLength(s: string) {
|
||||||
return Array.from(s).length;
|
return Array.from(s).length;
|
||||||
}
|
}
|
||||||
|
10
csv/parse.ts
10
csv/parse.ts
@ -12,7 +12,7 @@ import {
|
|||||||
type ReadOptions,
|
type ReadOptions,
|
||||||
type RecordWithColumn,
|
type RecordWithColumn,
|
||||||
} from "./_io.ts";
|
} from "./_io.ts";
|
||||||
import { graphemeLength } from "./_shared.ts";
|
import { codePointLength } from "./_shared.ts";
|
||||||
|
|
||||||
export { ParseError, type ParseResult, type RecordWithColumn };
|
export { ParseError, type ParseResult, type RecordWithColumn };
|
||||||
|
|
||||||
@ -109,7 +109,7 @@ class Parser {
|
|||||||
if (!this.#options.lazyQuotes) {
|
if (!this.#options.lazyQuotes) {
|
||||||
const j = field.indexOf(quote);
|
const j = field.indexOf(quote);
|
||||||
if (j >= 0) {
|
if (j >= 0) {
|
||||||
const col = graphemeLength(
|
const col = codePointLength(
|
||||||
fullLine.slice(0, fullLine.length - line.slice(j).length),
|
fullLine.slice(0, fullLine.length - line.slice(j).length),
|
||||||
);
|
);
|
||||||
throw new ParseError(startLine + 1, lineIndex, col, ERR_BARE_QUOTE);
|
throw new ParseError(startLine + 1, lineIndex, col, ERR_BARE_QUOTE);
|
||||||
@ -149,7 +149,7 @@ class Parser {
|
|||||||
recordBuffer += quote;
|
recordBuffer += quote;
|
||||||
} else {
|
} else {
|
||||||
// `"*` sequence (invalid non-escaped quote).
|
// `"*` sequence (invalid non-escaped quote).
|
||||||
const col = graphemeLength(
|
const col = codePointLength(
|
||||||
fullLine.slice(0, fullLine.length - line.length - quoteLen),
|
fullLine.slice(0, fullLine.length - line.length - quoteLen),
|
||||||
);
|
);
|
||||||
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
|
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
|
||||||
@ -164,7 +164,7 @@ class Parser {
|
|||||||
if (r === null) {
|
if (r === null) {
|
||||||
// Abrupt end of file (EOF or error).
|
// Abrupt end of file (EOF or error).
|
||||||
if (!this.#options.lazyQuotes) {
|
if (!this.#options.lazyQuotes) {
|
||||||
const col = graphemeLength(fullLine);
|
const col = codePointLength(fullLine);
|
||||||
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
|
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
|
||||||
}
|
}
|
||||||
fieldIndexes.push(recordBuffer.length);
|
fieldIndexes.push(recordBuffer.length);
|
||||||
@ -174,7 +174,7 @@ class Parser {
|
|||||||
} else {
|
} else {
|
||||||
// Abrupt end of file (EOF on error).
|
// Abrupt end of file (EOF on error).
|
||||||
if (!this.#options.lazyQuotes) {
|
if (!this.#options.lazyQuotes) {
|
||||||
const col = graphemeLength(fullLine);
|
const col = codePointLength(fullLine);
|
||||||
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
|
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
|
||||||
}
|
}
|
||||||
fieldIndexes.push(recordBuffer.length);
|
fieldIndexes.push(recordBuffer.length);
|
||||||
|
@ -199,7 +199,7 @@ Deno.test({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
await t.step({
|
await t.step({
|
||||||
name: "error column grapheme number",
|
name: "error column Unicode code point number",
|
||||||
fn() {
|
fn() {
|
||||||
const input = `a,b,🐱"`;
|
const input = `a,b,🐱"`;
|
||||||
assertThrows(
|
assertThrows(
|
||||||
|
Loading…
Reference in New Issue
Block a user