My most readable function
-
Reading through some of my older code, I found this gem (uncommented):
function fmt( str, ...args ) { return str.replace( /(\{)?(\{(\d+)\})(\})?/g, ( $0, $1, $2, $3, $4 ) => ( $1 && $4 ) ? $2 : ( $1 || '' ) + args[ +$3 ] + ( $4 || '' ) ); }
It's ECMAScript 6. It took me a couple minutes to decipher. Can you figure it out?
Filed under: Not my proudest moment.
-
It's designed to make everybody who touches the codebase in the future angry at you.
It does nothing.
-
Well, all i know is it would look much better as 50 lines of loops and ifs/switches nested 10 levels deep.
-
taking a NSSWAG here that it dows some parsing and reformatting of a JSON string?
-
The intent is that e.g.
fmt( '{0} {{1} {2}} {{2}}', 'foo', 'bar', 'baz' )
producesfoo {bar baz} {2}
(which it actually does correctly). So replace braced number with the argument of that number, unless the braces are doubled.For those who know Javascript but not ES6, the ES6 features used here are
...
to implicitly turn the argument list into an array, and the=>
operator which works like it does in C#.OK, so I use the string replace method with a callback for each capture. There are 4 capture groups:
- $1: the optional escaping {
- $2: the inner braces and number
- $3: the number
- $4: the optional escaping }
If both $1 and $4 are present, then the braces are escaped, so return $2 (the inner brace and number). Otherwise, return the argument with index $3, using the
+
operator to cast to number. If the argument is half-escaped, then make sure you put the non-escaping brace back in.Such maintainable.
Very code.
Filed under: Why am I casting to number? Array indices in Javascript are strings, like all properties.
-
What was the intent for this algorithm given "{{{0} {1}}}" I wonder?
Isn't TRWTF using regular expressions to do recursive parsing?
-
What was the intent for this algorithm given "{{{0} {1}}}" I wonder?
The intent was to ignore all braces that are not part of a braced number.
So desired output would be
{{foo bar}}
.Isn't TRWTF using regular expressions to do recursive parsing?
It's not recursive, nor should it be.
-
It's not recursive, nor should it be.
nope, but i am stealing this for later. i forgot replace works with functions. that'll simplify some shadowmod code.
-
Perhaps a more important question than "how does it work?" is "is it necessary?".
Looks like you were trying to replicate the behavior of .Net's String.Format.
Is there no javascript library that has this?
-
Perhaps a more important question than "how does it work?" is "is it necessary?".
That's fair. Probably not. ES6 has format strings already, though this was in some code doing some reflection-y stuff so the format string wasn't known before runtime.There's a reason I posted it as a WTF.
@Bort said:Looks like you were trying to replicate the behavior of .Net's String.Format.
That was the inspiration. I know .NET's is more robust, too, allowing e.g.: {0:yyyy-MM-dd} but that seemed like overkill.Is there no javascript library that has this?
I'm sure there is, but this code has zero external dependencies and I didn't feel like raising that by an infinite multiple.
-
nope, but i am stealing this for later. i forgot replace works with functions. that'll simplify some shadowmod code.
Please, include some of my explanatory post in a comment.
-
-
I release this "wonderful" code snippet into the public domain.
Filed under: Is there a FOSS license where you can only use the code if you don't attribute it to me?
-
I'm sure you've heard this a thousand times, but...
@redwizard, you look like a guy who needs food badly.
-
I release this "wonderful" code snippet into the public domain.
Filed under: Is there a FOSS license where you can only use the code if you don't attribute it to me?
Look at it this way. A decade or so from now, when you're training up a neophyte in the field and they make some really WTFy code that depresses them, you can show them your code as an example why they have no reason to give up hope. ;-)
-
@redwizard, you look like a guy who needs food badly.
Reminds me of the old multi-player video game Gauntlet, when your character's health falls below 200.
Edit: ahh, memories:
Gauntlet - Arcade – 04:31
— classicgamevideos
-
-
Ugh. Thank you wikia, I thought you allowed hotlinking.
-
@redwizard, you look like a guy who needs food badly.
And I was all like: "but his avatar looks like some kind of spaceship "
-
Reminds me of the old multi-player video game Gauntlet, when your character's health falls below 200.
Seems like an opportunity for a whoosh flag....
-
This guy?
-
-
I release this "wonderful" code snippet into the public domain.
Filed under: Is there a FOSS license where you can only use the code if you don't attribute it to me?
You just missed a chance to use the WTF Public License. Then again, it says nothing about your extra terms.
-
I wonder if @error's hypothetical no-attribution license would count as a Free software license according to the FSF ...
-
ISTRT the "public domain" license does....so.... probably?
-
I think not—only GPL or a derivative is acceptable to the FSF.
-
"No restrictions of any kind? That's not free enough."
-
Well, yes.
- "Our Freedom™ is the True Freedom"
- "Freedom that can be properly used by people who hate Freedom™ isn't True Freedom"
-
- Oceania has always been at war with
EastasiaEurasia.
- Oceania has always been at war with
-
Um, no.I wonder if @error's hypothetical no-attribution license would count as a Free software license according to the FSF ...
I think not—only GPL or a derivative is acceptable to the FSF.We classify a license according to certain key criteria:
- Whether it qualifies as a free software license.
- Whether it is a copyleft license.
- Whether it is compatible with the GNU GPL. Unless otherwise specified, compatible licenses are compatible with both GPLv2 and GPLv3.
- Whether it causes any particular practical problems.
-
GPL
Um, no....GNU GPL...GPLv2 and GPLv3.
Oh actually, your wright, you can basically do whatever you wanted with no legal qualms whatsoever.
-
Huh? I actually have no idea what you're trying to say.
@aliceif wondered if the FSF would call the public domain declaration Free. You said they wouldn't, and only GPL and derivatives are acceptable.
I pointed out that they classify licenses by (1) freedom and (2) GPL-compatibility. These are different, which is why they are listed differently. There are plenty of FSF-declared "Free" licenses that are not only weaker than the GPL (like CC0) but that several GPL-_in_compatible licenses are considered "Free" by the FSF.
Of course they state a preference for GPL-compatible and copyleft when possible -- but their definition of Free certainly doesn't demand either.
-
-
* Oceania has always been at war with
EastasiaEurasiaNot quite the effect I intended when I put that CSS in there, but.. oh well.
-
"No restrictions of any kind? That's not free enough."
Yeah, because it's possible to make it non-free.
-
Yeah, because it's possible to make it non-free.
and yet public domain (which can be used in non-free) is approved by the FSF (although they do prefer you use a mostly equivilant CC0 license which is basically "if it's legal for this thing to be in public domain, it's in public domain, otherwise heres a super permissive license that lets you do whatever the heck you want"
-
The FSF is a useful thing to have around, but they're kinda crazy.
-
Public domain isn't a license, really. The owner releases any claim she may have had of ownership. It's no longer her's to license.
Filed under: Feminine pronouns, why not?
-
-
Huh? I actually have no idea what you're trying to say.
I made the point that some hypothetical license would probably not be acceptable as a "free software" license to the FSF, because it lacks several of the clauses necessary for an FSF-style licence (forcing distribution of the sourcecode along with the binaries, for example).
I made this point in an admittedly imprecise, conversational way. You then made a spirited attempt for a pedantry badge at about 11:55pm my local time, which didn't really give me time to make any kind of sensible response until about now.
-
-
The phrase "compatible with the GPL" always makes me grind my teeth.
Mainly because the opposite question is never addressed: Using GNU's own definition, what licenses is the GPL compatible with?
Answer: None, other than possibly other versions of the GPL. The GPL is intentionally written this way.
-
Mainly because the opposite question is never addressed: Using GNU's own definition, what licenses is the GPL compatible with?
It's true of any license that allows you to re-license under the GPL without the copyright holder's permission, which is plenty of non-GPL free licenses.Answer: None, other than possibly other versions of the GPL. The GPL is intentionally written this way.
As evidenced by the FSF listing 46 non-GPL licenses under the GPL-compatible category.
-
My point being is that you can't take GPL code and include it in, say, an Apache License 2.0 project. Because GPL explicitly forbids relicensing.
-
My point being is that you can't take GPL code and include it in, say, an Apache License 2.0 project. Because GPL explicitly forbids relicensing.
Yes, you can do that [for GPL3 code]. It's just that the conglomerate will be subject to the GPL instead of Apache license. You can still release the other parts of the project under the Apache license.If that's not acceptable because you want the whole project Apache, then you can't do it, but that's your decision, not the licenses being incompatible.
I'm not trying to argue that the GPL or FSF is "right", nor do I have a problem with describing the GPL's requirements as "viral", nor do I acknowledge that there are some combinations that one might like to make of GPL + non-GPL code that isn't permitted, such as GPL+CDDL or GPLv2+Apache. But to say that the only GPL-compatible license is the GPL & variants is... taking a pretty useless definition of "compatible" IMO.
-
Hence the common phrase that the GPL is a vampire.
-
Yes, you can do that [for GPL3 code]. It's just that the conglomerate will be subject to the GPL instead of Apache license.
Actually, you can't (in a pedantic sense). Though it's quite possible to make the software release, you can't have the result be part of an Apache project. Software released by Apache projects always uses the Apache license. The act of releasing the thing with the GPL bit in it makes it non-Apache. However, that doesn't flow backwards into the rest of the code so long as it is meaningfully usable without the GPL code: since there's no reasonable sense in which the Apache code could be said to be a derivative work of the GPL code (unlike the aggregation of the two), license infectivity is denied.
(I've got a project in the Apache Incubator process right now. I know what the licensing restrictions are. The code in question used to be LGPL, but we're changing the license. We can do that: we've got the copyright assignments on file. )
-
Actually, you can't (in a pedantic sense).
Actually, you can; you would just be in violation of the license terms.
The code in question used to be LGPL, but we're changing the license. We can do that: we've got the copyright assignments on file.
As I understand it, you can't revoke the existing license you've granted, but that only covers the exact version you released. You, as the copyright owner, are free to license any other versions under any terms you choose, or to dual license the existing version.
Filed under: I ANAL
-
you can't have the result be part of an Apache project
No one said anything about making it a project under the Apache organization. Besides, I even specifically addressed the case you're talking about: if you can't include GPL code in an Apache project because of the GPL license, that's not because the GPL code is incompatible with your code's license, it's incompatible with your goals/requirements.
-
Using GNU's own definition, what licenses is the GPL compatible with?
Answer: None, other than possibly other versions of the GPL.
@http://gplv3.fsf.org/rms-why.html said:
When we say that GPLv2 and GPLv3 are incompatible, it means there is no legal way to combine code under GPLv2 with code under GPLv3 in a single program. This is because both GPLv2 and GPLv3 are copyleft licenses: each of them says, “If you include code under this license in a larger program, the larger program must be under this license too.” There is no way to make them compatible. We could add a GPLv2-compatibility clause to GPLv3, but it wouldn't do the job, because GPLv2 would need a similar clause.
Post can't be empty