我正在使用 function main() {
const sheet = SpreadsheetApp.getActiveSheet();
const range = sheet.getRange("A1:B");
const data = range.getValues().map(x => [x[0], crc_ccitt_ffff(x[0])]);
range.setValues(data);
}
function crc_ccitt_ffff(str) {
if (str == '') return;
const get_crc_for_num = (n) => {
let crc = 0;
let c = n * 256;
[...Array(8).keys()].forEach(_ => {
crc = ((crc ^ c) & 0x8000) ? (crc * 2) ^ 0x1021 : crc * 2;
c = c * 2;
});
return crc;
}
const table = [...Array(256).keys()].map((_,n) => get_crc_for_num(n));
var crc = 0xFFFF;
str.split('').forEach(c => {
crc = (crc * 0x100) ^ table[(((crc / 0x100) >> 0) ^ c.charCodeAt()) & 0xFF];
crc = (((crc / 0x10000) >> 0) * 0x10000) ^ crc;
});
return crc.toString(16);
}
函数。我想知道如何让它返回实际的字符串名称而不是布尔值。我也愿意使用其他功能。
startsWith
谢谢!
答案 0 :(得分:3)
代替 startsWith
,使用 grep
即时返回值,并使用 ^
指定字符串的开头(编辑 - 基于 @Ben Bolker 评论)
grep("^happy", c("sad_game", "angry_mad", "happy_name"), value = TRUE)
[1] "happy_name"
startsWith
返回一个逻辑向量。我们需要以逻辑向量作为索引对原始向量进行子集
c("sad_game", "angry_mad", "happy_name")[startsWith(c("sad_game",
"angry_mad", "happy_name"), "happy")]
注意,在上面,我们必须输入原始向量两次。更好的选择是创建一个对象并重用它
v1 <- c("sad_game", "angry_mad", "happy_name")
v1[starts_with(v1, "happy")]
答案 1 :(得分:0)
另一种选择是使用 jest.mock("moment", () => {
// Require actual moment
const actualMoment = jest.requireActual("moment");
// Mocking moment func:
// moment() => return specific date, and it won't affect moment(date) with param.
const mockMoment: any = (date: string | undefined) =>
actualMoment(date || "2021-01-01T00:00:00.000Z");
// Now assign all properties from actual moment to the mock moment, so that they can be used normally
for (let prop in actualMoment) {
mockMoment[prop] = actualMoment[prop];
}
return mockMoment;
});
中的 str_subset
stringr
基于 x <- c("sad_game", "angry_mad", "happy_name")
stringr::str_subset(x, '^happy')
#[1] "happy_name"
stringi::stri_subset_regex