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