buffer.isLatin1(input)
如果 input 可以使用 Node.js 的 'latin1' 编码无损编码(包括 input 为空的情况),这个函数将返回 true。也就是说,如果 input 的每个 UTF-16 代码单元都在 U+0000 到 U+00FF 范围内,它就会返回 true。这样的字符串也是一个有效的 WebIDL ByteString。
🌐 This function returns true if input can be losslessly encoded using the
Node.js 'latin1' encoding, including the case in
which input is empty. That is, it returns true if every UTF-16 code unit
of input is in the range U+0000 to U+00FF. Such a string is also a valid
WebIDL ByteString.
这个检查使用 Node.js 中 'latin1' 的定义,其中每个从 U+0000 到 U+00FF 的代码单元都直接映射到相同数值的字节。它不使用 WHATWG 编码标准 的定义,其中 'latin1' 标签是 windows-1252 的别名。例如,'\u0080' 在这个函数里被认为是 latin1,而 '€'(U+20AC,由 windows-1252 编码为 0x80)则不是。
🌐 This check uses the Node.js definition of 'latin1', in which each code unit
from U+0000 to U+00FF maps directly to the byte of the same value. It does
not use the WHATWG Encoding Standard definition, in which the 'latin1'
label is an alias for windows-1252. For example, '\u0080' is considered
latin1 by this function, while '€' (U+20AC, which windows-1252 encodes as
0x80) is not.
与 buffer.isAscii() 和 buffer.isUtf8() 不同,这个功能验证的是字符串,而不是 Buffer、TypedArray 或 ArrayBuffer。每个字节序列都很容易成为有效的 'latin1',因为每个字节都映射到小于或等于 0xFF 的代码单元。
🌐 Unlike buffer.isAscii() and buffer.isUtf8(), this function
validates a string rather than a Buffer, TypedArray, or ArrayBuffer.
Every byte sequence would trivially be valid 'latin1', since every byte maps
to a code unit less than or equal to 0xFF.
import { isLatin1 } from 'node:buffer';
isLatin1('hello'); // true
isLatin1('café'); // true
isLatin1('\u00ff'); // true
isLatin1('\u0100'); // false
isLatin1('€'); // falseconst { isLatin1 } = require('node:buffer');
isLatin1('hello'); // true
isLatin1('café'); // true
isLatin1('\u00ff'); // true
isLatin1('\u0100'); // false
isLatin1('€'); // false