I am porting some C code to JavaScript. I would like to not have to implement basic functions like strcspn myself
const strcspn = (a, b) => {
for (let [index, c] of [...a].entries()) {
if (b.includes(c)) {
return index
}
}
return a.length
}
Is there a project I can copy them from or that I could import?
There is no library in Javascript that provides the same functions of the C Standard Library (that I'm aware of, at least).
So you have to rewrite the functions yourself (not particularly difficult, as Javascript has many builtins that may help you).
Better yet: don't use C standard functions in Javascript: modify the C code directly in order to write more idiomatic Javascript code. What I mean:
// a C code that uses `strtok()` to split a string may be rewritten in JS as:
const splitted = str.split(" ");
// it's cleaner and you don't need to rewrite `strtok()` in JS
NOTE: your implementation of strcspn() is wrong. The correct implementation in Javascript would be:
function strcspn(dest, src) {
let res = 0;
for (let i = 0; i < dest.length; i += 1) {
if (src.includes(dest.charAt(i))) {
break;
}
res += 1;
}
return res;
}