π I was recently wondering why should I keep doing that for every new project idea, so I made a ts npm package to just import my own code everywhere.
You should do a post on this. I use JavaScript all the time but I'm seeing syntax I'm not familiar with. I feel like I'm peeking through a window from a room I didn't know I was stuck in.
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
π Both @c_basso and @fedyk solutions are coded in TS that can be the cause of your confusion @kevinleebuchan, here's a JS translation (guys correct me if I miss something)
/** Retrieves Ditto data */constgetDitto=async()=>{const[result,error]=awaitgo(fetch('https://pokeapi.co/api/v2/pokemon/ditto'));if(error)console.error(error);elseconsole.log(awaitresult.json());};awaitgetDitto();
Note that the return value of the go function is always an array with two positions, the first one intended to be a successful result, the second one reserved for the error so in case it succeeds it sends [result, null] and in case it fails it sends back [null, error], this may help to understanding:
exporttypePartialBy<T,KextendskeyofT>=Omit<T,K>&Partial<Pick<T,K>>;exporttypeMaybeNull<T>=T|null;exporttypeMaybeUndefined<T>=T|undefined;exporttypeMaybeNil<T>=T|null|undefined;exporttypeAsyncFn=(...args:any[])=>Promise<any>;exporttypeAnyFn=(...args:any[])=>any;exporttypeUnboxPromise<TextendsPromise<any>>=TextendsPromise<inferU>?U:never;exporttypeNullable<T>={[PinkeyofT]:MaybeNull<T[P]>;};exporttypeNullableBy<T,KextendskeyofT>=Omit<T,K>&Nullable<Pick<T,K>>;exporttypeDistributedArray<U>=Uextendsany?U[]:never;/**
* type ID = string | number;
* type T0 = ID[];
* type T1 = DistributedArray<ID>;
* type EQ1 = IfEquals<T0, T1, 'same', 'different'>; // different
*/exporttypeIfEquals<T,U,Y=unknown,N=never>=(<G>()=>GextendsT?1:2)extends<G>()=>GextendsU?1:2?Y:N;exporttypeOptional<T,KextendskeyofT>=Pick<Partial<T>,K>&Omit<T,K>;exporttypeValueOf<T>=T[keyofT];
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
This is one I made and using across many projects.
/**
* Takes full name and converts into initials
* @param {string} string full name of user e.g John Doe
* @returns initials e.g JD
*/constgetInitials=(string='')=>string.split('').map(([firstLetter])=>firstLetter).filter((_,index,array)=>index===0||index===array.length-1).join('').toUpperCase()
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
You must restrict your initials to some letter count either one or two, see your initial from the above fn will grow with no. of words in a name which is not favourable to be called 'initials'
And yes my country surnames is at last so picking the last one in my code π
For input like Mary Jane Watson, your function returns MJW, and his returns MW.
It's common practice to use only two letters as initials on avatars (eu.ui-avatars.com/api/?name=Mary+J...)
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
That was one of my first thoughts but initials depend on the country, so my name is Joel Bonet Rxxxxx, being Joel my name and Bonet + Rxxxxx my two surnames.
So in this case I'll be JR with this approach which in my country and according to my culture or what is reasonable, it should be JB. (first character of the name + first character of the surname).
That's the main reason for getting separate fields for name and surname/s.
So your name can be "Mary Jane Linda" and your surnames can be "Lee Bonet".
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
Of course but what I want to check is the details on what's happening on this TextDecoder(), it's decode method and also the reason for it to use an array of 8-bit unsigned integers π
TextEncoder and TextDecoder convert strings to and from their equivalent UTF-8 bytes. So for example, new TextEncoder().encode('foo η¦ π§§') gives Uint8Array [102, 111, 111, 32, 231, 166, 143, 32, 240, 159, 167, 167], where bytes 0..2 represent the 3 ASCII characters "foo", 4..6 represent the single 3-byte character "η¦", and 8..11 represent the single 4-byte character "π§§" (3 and 7 are spaces).
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
I've been a professional C, Perl, PHP and Python developer.
I'm an ex-sysadmin from the late 20th century.
These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
I don't really have snippets I reuse. I might occasionally pinch a function from another project, but there's nothing I can think of that would be worth a snippet, and I've never really remembered to use them when I've tried.
I do have these vim mappings for older PHP projects which don't have much in the way of development tools:
They'll let me paste in a variable dump with my cursor positioned where I need to put in the thing to debug, or a generic stack trace. They also cope with frameworks that do tricky things with output buffering.
Latest comments (52)
none, I publish my standard library to public repos and import it.
π I was recently wondering why should I keep doing that for every new project idea, so I made a ts npm package to just import my own code everywhere.
Take a look if you want: npmjs.com/package/js-math-and-ui-u...
I work a lot with animations and free-hand user input so one often use is
getQuadraticBezierCurvePointAtTime:The identity function in any language that doesn't have it.
I see very experienced coders here. It would be great if anyone could guide me with my little project.π₯²
Function for prevent using
try catchin all async await callsand use it like this
You should do a post on this. I use JavaScript all the time but I'm seeing syntax I'm not familiar with. I feel like I'm peeking through a window from a room I didn't know I was stuck in.
π Both @c_basso and @fedyk solutions are coded in TS that can be the cause of your confusion @kevinleebuchan, here's a JS translation (guys correct me if I miss something)
Usage example:
Note that the return value of the
gofunction is always an array with two positions, the first one intended to be a successful result, the second one reserved for the error so in case it succeeds it sends[result, null]and in case it fails it sends back[null, error], this may help to understanding:Hope it helps! π
to- funny name. I use similar helper, but I call itgo. Here my recent post about it dev.to/fedyk/golang-errors-handing...utils/ts-utility-types.ts:C'mon show us some functions π€£π€£
I'm not sure about those TypeDefs honestly, I think it's better to have an inline, let's say:
so it's more clear for everyone than saying
but to keep the honesty in this comment, this is opinionated and I may be wrong π
This is one I made and using across many projects.
It seems a bit overcomplicated to me, I'd do something like:
I think it does the same but... Am I missing something maybe? Probably some use case that is not in my mind π
You must restrict your initials to some letter count either one or two, see your initial from the above fn will grow with no. of words in a name which is not favourable to be called 'initials'
And yes my country surnames is at last so picking the last one in my code π
For input like
Mary Jane Watson, your function returnsMJW, and his returnsMW.It's common practice to use only two letters as initials on avatars (eu.ui-avatars.com/api/?name=Mary+J...)
That was one of my first thoughts but initials depend on the country, so my name is Joel Bonet Rxxxxx, being Joel my name and Bonet + Rxxxxx my two surnames.
So in this case I'll be JR with this approach which in my country and according to my culture or what is reasonable, it should be JB. (first character of the name + first character of the surname).
That's the main reason for getting separate fields for name and surname/s.
So your name can be "Mary Jane Linda" and your surnames can be "Lee Bonet".
and still getting ML as initials which would be the most correct output as far as I can tell.
Here I am enjoying all the great, open conversations about programming and now I learn something about other cultures too. #Winning
Here are a few of the more generally useful ones:
Those are currently very useful ones!
I've a slightly different base64encode one
I'll check the differences later π
The base64 functions from @lionel-rowe are compatible with (most) browser. The fs module is only available in Node.js :)
Of course but what I want to check is the details on what's happening on this
TextDecoder(), it'sdecodemethod and also the reason for it to use an array of 8-bit unsigned integers πTextEncoderandTextDecoderconvert strings to and from their equivalent UTF-8 bytes. So for example,new TextEncoder().encode('foo η¦ π§§')givesUint8Array [102, 111, 111, 32, 231, 166, 143, 32, 240, 159, 167, 167], where bytes 0..2 represent the 3 ASCII characters "foo", 4..6 represent the single 3-byte character "η¦", and 8..11 represent the single 4-byte character "π§§" (3 and 7 are spaces).Thanks you for the explanation! π
let callApi=(path) => fetch(path).then(response => response.json())
This is my favourite one and universal too:
This will work just for GET requests without authentication, let me do a little tweak on it by just adding an optional "options" parameter π
services/api/utils.jsnow you can perform any HTTP verb request on a new layer of functions like
PUT example (POST will look mostly the same):
services/api/users.js:Working example
You can try it in your browser terminal right now:
services/api/pokemon.js:usage:
I don't really have snippets I reuse. I might occasionally pinch a function from another project, but there's nothing I can think of that would be worth a snippet, and I've never really remembered to use them when I've tried.
I do have these vim mappings for older PHP projects which don't have much in the way of development tools:
They'll let me paste in a variable dump with my cursor positioned where I need to put in the thing to debug, or a generic stack trace. They also cope with frameworks that do tricky things with output buffering.
That's about it.