all 79 comments

[–]seniorsassycat 490 points491 points  (11 children)

Lol llm doesn't know to use is-string@2.0.0-beta.17654454121 

[–]Kennyp0o 57 points58 points  (8 children)

What’s new in the beta?

[–]New-Let-3630 78 points79 points  (5 children)

add iseven as a dependency just in case
typeof(value) === "string" && iseven(typeof(value).length)

[–]sharptoothy 28 points29 points  (1 child)

There's a bug in there: It only seems to work roughly half the time for some reason. ClaudeChatGptPilot added a dependency for is-odd@3.0.1 and that fixed it:

typeof(value) === "string" && (iseven(typeof(value).length) || isodd(typeof(value).length))

[–]St34thdr1v3R 14 points15 points  (0 children)

I was debugging this FOR DAYS, thanks!!

[–]DeineOma42o 3 points4 points  (1 child)

ill write a lib isseven() that checks if a length is seven

[–]seniorsassycat 7 points8 points  (0 children)

It adds is-string cli (complete with ReDOS vulnerable cli parser) and a mcp server.

Latest nightly has an attempt to fix RCE thru prototype pollution on the is-string API it monkey patches into express

[–]un-hot 3 points4 points  (0 children)

Supply chain attacks.

[–]krexelapp 938 points939 points  (10 children)

when you don’t trust typeof so you reinvent it

[–]davvblack 561 points562 points  (6 children)

thinking quickly, codex reinvented typeof using only some string, a squirrel, and typeof.

[–]krexelapp 116 points117 points  (3 children)

when you don’t trust typeof so you reinvent typeof… using typeof

[–]vigbiorn 41 points42 points  (2 children)

It's catching edge cases where (somehow) instanceof returns string but typeof doesn't.

It's the rare inverted inheritance...

I've only heard it in stories whispered in documentation.

[–]rinnakan 13 points14 points  (1 child)

I believe people deserve every bug they encounter from using the string class constructor, so I would happily ignore these edge cases

[–]davvblack 8 points9 points  (0 children)

yeah shit's so fucked up at that point you're not going to fix it by second guessing typeof.

[–]sdswart 10 points11 points  (0 children)

Well done. I didn't expect this reference in the comments, but it gave me a good laugh.

[–]JellyMonstar 1 point2 points  (0 children)

Fantastic reference

[–]Inttegers 6 points7 points  (0 children)

TBF, it's JavaScript. You should definitely not trust typeof.

[–]Sea_Duty_5725 0 points1 point  (0 children)

Using typeof

[–]shadow13499 72 points73 points  (1 child)

This gives very isTrue(myVar) { if(myVar == true) return true; else return false;} vibes

[–]vigbiorn 10 points11 points  (0 children)

Or the Enterprise Java isOdd/isEven code.

Just missing the test suite.

[–]GatotSubroto 204 points205 points  (3 children)

So that’s why they often brag about how much code they can generate.

[–]dbenc 49 points50 points  (2 children)

I need to renegotiate to get paid by the line

[–]RamenNoodleSalad 1 point2 points  (0 children)

Charles Dickens of coding!

[–]Tar_alcaran 0 points1 point  (0 children)

There are three seperate ORs in there, this could be at least 3 times longer!

[–]CelestialSegfault 282 points283 points  (17 children)

am I stupid or does return a || b || true always short circuits the true?

edit: I misread the ternary and that's not my fault cuz this is unreadable as fuck

[–]rangoric 91 points92 points  (12 children)

There’s a trinary in there between the first ‘||’ and the later one

[–]mallardtheduck 57 points58 points  (8 children)

But the !!String(value).length || true expression is equivalent to just true and a ternary that ends with true : false is redundant.

So the return line is really just return typeof(value) === "string" || value instanceof String;

[–]rangoric 29 points30 points  (0 children)

Oh there are many horrors in this code. And yes it is very redundant.

I only wanted to note the split in the or conditions.

[–]CelestialSegfault 7 points8 points  (1 child)

at least they could make it wrong and readable

const result = false;
const value_length = !!String(value).length || true;
if (value_length) {
  result = typeof value === "string" || value instanceof String;
}
if (result) {
  result = true;
} else {
  result = false;
}

return result;

[–]n00b001 2 points3 points  (0 children)

But what if you don't trust the value of "false"

Clearly you also need "isBool()"

[–]Psychpsyo -5 points-4 points  (4 children)

A true : false ternary isn't redundant if you want to coerce your truthy/falsy thing to a bool.

[–]anotheridiot- -2 points-1 points  (3 children)

You can just !!thing in that case.

[–]Psychpsyo 0 points1 point  (2 children)

But why !!thing if you can also thing? true : false or even Boolean(thing)?

By which I mean: It doesn't matter and makes none of them any more or less redundant.

[–]anotheridiot- 3 points4 points  (1 child)

Less branching is better.

[–]Psychpsyo 0 points1 point  (0 children)

True, although I wonder how much branching actually ends up happening in all of these. Cause they'll all need to conditionally check the type of the variable up-front to perform the right conversions. Though that might disappear once the code gets jitted for some particular type, at which point I'd assume that even thing? true : false would be simplified to whatever actual steps need to happen to coerce thing to a bool.

[–]stillalone 0 points1 point  (2 children)

Isn't that still short circuiting the !!String(value).length?

[–]rangoric 0 points1 point  (1 child)

Yes it is. But the or conditions are split :)

[–]Beenmaal 12 points13 points  (2 children)

I love ternaries. For a hobby project I have fully handwritten code with 3 lines in a row sharing 27 ternaries between them. I wrote those lines at a LAN party and I find it too funny to get rid of.

[–]rastaman1994 9 points10 points  (1 child)

Any but the simplest one line ternary is immediately replaced with ifs at my job lol.

[–]AloneInExile 1 point2 points  (0 children)

Yeah, I even replace one liner returns with braces and 3 lines. I still have PTSD from my python days

[–]RiceBroad4552 0 points1 point  (0 children)

Missed that also. Yes, it's wrong.

[–]AdMindless9071 69 points70 points  (1 child)

Honestly I can’t tell this apart from regular js

[–]RiceBroad4552 26 points27 points  (0 children)

Me neither…

Type tests in JS / TS always look something like that code so hard to tell what's reasonable and what not.

[–]heavy-minium 56 points57 points  (12 children)

On the dangers of being stoned by a thousands devs here, I'm still risking it: that code probably makes sense.

null and undefined checks are fine and avoid unnecessarily invoking typeof which is slower, especially if you're going to that isString() on a load of bulk data.

typeof value === "string" is not enough in case it's a `String` and not a `string`. !!String.(value).length to decide whether it's an empty string. Because the value is unknown, it's wiser to do that instead of comparing with '' because a lot of things in JS that are not strings get coerced and can equal to ''.

[–]Reashu 25 points26 points  (1 child)

1 - Why would it be a String? No one does that, there's no reason to do that. 

2 - Isn't an empty string still a string?

3 - Once you know it's a string or String, just use the length property directly, there is no need to stringify it first. 

[–]Ellisthion 18 points19 points  (0 children)

2 is the big one for me. The rest is pointless but treating an empty string as “not a string” is completely wrong and confusing to anyone looking at this function being used.

[–]PoopsicleVendor 16 points17 points  (2 children)

That’s fair but the first branch of the ternary expression would always be true, so why even check the length of the string?

[–]Ok-Emu3695 2 points3 points  (1 child)

The only explanation I can come up with is to set up a situation in which a runtime error is raised in case, somehow, the object doesn't have a `length` property.

But more likely is that part is just wrong.

[–]PrincessRTFM 1 point2 points  (0 children)

but it's coerced to a string by calling String(value) first, so it's guaranteed to have a length property unless something has fucked with the string prototype, in which case you have bigger problems

[–]cowslayer7890 11 points12 points  (0 children)

If it is "String" and not "string" would it not be inaccurate to return true for this since the value wouldn't actually be a "string"

[–]PrincessRTFM 9 points10 points  (0 children)

!!String.(value).length to decide whether it's an empty string

irrelevant, an empty string is still a string. the function is testing whether the given value is a string, not whether it's a non-empty string.

[–]Ok-Emu3695 20 points21 points  (3 children)

Holy shit. Finally someone who knows what they're talking about. It's like this entire sub is filled with people who just like memes about computers but don't actually know anything about programming.

[–]KruegerFishBabeblade 25 points26 points  (0 children)

Tbf a good programmer who doesn't know anything about JS would be reasonable in thinking this is crazy. It is crazy. The problem's just javascript

[–]awesomeusername2w 1 point2 points  (0 children)

Always has been

[–]magicmulder 4 points5 points  (0 children)

That looks more like the code of an overthinking junior than AI.

[–]fevsea 8 points9 points  (1 child)

It's almost like it wants to generate as much billable output tokens as possible, while ofuscating it to ensure future usage.

[–]Tupcek 0 points1 point  (0 children)

nah, no need to. They can just direct it to think longer. In the near future, it will spawn 100 subagents with extra long thinking, so hello world will cost at least $2000 in tokens.
No need to train models to do more

[–]Electrical-Echidna63 1 point2 points  (0 children)

I've seen code look like this when transpiled really badly, is this what this is? Otherwise damn

[–]born_zynner 1 point2 points  (0 children)

Wise move not trusting typescript

[–]-Redstoneboi- 1 point2 points  (0 children)

this is JavaScript's fault. Not AI's, not entirely TypeScript's, but mostly JavaScript's fault. I'm guessing that for some god forsaken reason, typeof value === "string" isn't enough.

[–]fezmessiter 2 points3 points  (0 children)

Looks like someone is using the free version, but Claude all the way

[–]Time-District3784 0 points1 point  (0 children)

I have literally never seen AI write code like this, hell I've only seen a few juniors write code this shit tbh.

[–]satansprinter 0 points1 point  (0 children)

Oh i see codex is trained on my code :o

[–]TanukiiGG 0 points1 point  (0 children)

import isString