I have an informal stash of useful regexes I copy and paste as needed. Informal meaning it's not quite organized enough for me to cleanly list them here, but it's sort of a system I have that has evolved naturally.
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
So this kind of things that are informal till you make a repo out of it because you've used it in like 30 projects and it has been standarized in some sort across your softwareπ
I have a framer motion animations file whith all pre coded I just need to import to the new project, thinking about to turn it into a Npm package, so it will be even easier to import
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 have an rng function that returns a random number between 1-x, and a randomizer function that returns a random item from an array-- the latter is probably used even more than the former!
I also set up a janky function to populate complicated dropdown menus based on arrays and nested arrays (element ID, array, dropdown type). I've ended up reusing it much more than anticipated, so should probably refactor it, ha.
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
Depends on the type of project.
For react websites I have a blocker (loading animation, waiter) which is 80% universal, maybe add a custom (s)css to style it.
I also keep a standard react-table setup that adds a layer upon react-table fixing the issues it has in typescript (types aren't extended when a function is added,)
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
Hahaha no judgements. Still I let you some polling thingy I had here that may be more suitable:
constpoll=async({fn,validate,interval,maxAttempts})=>{letattempts=0;constexecutePoll=async(resolve,reject)=>{constresult=awaitfn();attempts++;if(validate(result)){returnresolve(result);}elseif(maxAttempts&&attempts===maxAttempts){returnreject(newError('Exceeded max attempts'));}else{setTimeout(executePoll,interval,resolve,reject);}};returnnewPromise(executePoll);};
poll function is a higher-order function that returns a function, executePoll.
executePoll function returns a promise and will run recursively until the condition is met.
Args explained:
fn: an API request (or another async thingy that suits for this polling).
validate: Test function to see if the data matches what we want, in which case it will end the poll.
interval: To specify how much it wait between poll requests.
maxAttempts: how many tries before throwing an error.
I couldn't sleep last night and read this comment at 3am. Coincidentally, today, I needed this poll. Works great in my usEffect().
The only thing that caught me is that the parameters to the above poll function are in a class, instead of individual parameters.
Thanks for the code!
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
Didn't get the meaning of the sentence "The only thing that caught me is that the parameters to the above poll function are in a class, instead of individual parameters." still glad to see it helped you π
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
Oh! nothing to do with classes it's just it receives an object but this is opinionated as I prefer to have the object structure for those "config" thingies, you can simply delete the brackets on the function declaration like that:
Nice snippet.
The new Promise callback should not be an async function. By passing an async fn to new Promise youβre βdouble promisingβ.
Refactoring to not depend on resolve/reject would solve that.
Or maybe
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
Having it as async lets you handle the response, the error and any action to perform either it went OK or KO, so it can be customised specifically on it's environment.
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.
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
Oldest comments (52)
I have an informal stash of useful regexes I copy and paste as needed. Informal meaning it's not quite organized enough for me to cleanly list them here, but it's sort of a system I have that has evolved naturally.
So this kind of things that are informal till you make a repo out of it because you've used it in like 30 projects and it has been standarized in some sort across your softwareπ
I have a framer motion animations file whith all pre coded I just need to import to the new project, thinking about to turn it into a Npm package, so it will be even easier to import
That's a great thing to do! (and usually pretty convenient).
I just did it yesterday with this thing here. Got 200 downloads the first day so I guess others found it useful π
I'll need to wait till next Sunday for the weekly stats to update
Can you share it with us? Framer motion is a great package tho
@mangor1no I have to implement it, and for sure I will, and it will be open for contributions too
I have an rng function that returns a random number between 1-x, and a randomizer function that returns a random item from an array-- the latter is probably used even more than the former!
I also set up a janky function to populate complicated dropdown menus based on arrays and nested arrays (element ID, array, dropdown type). I've ended up reusing it much more than anticipated, so should probably refactor it, ha.
We all have those
TODO: Refactor this function/method
at the bottom of our TODO list πDepends on the type of project.
For react websites I have a blocker (loading animation, waiter) which is 80% universal, maybe add a custom (s)css to style it.
I also keep a standard react-table setup that adds a layer upon react-table fixing the issues it has in typescript (types aren't extended when a function is added,)
The
rafce
shortcut is a life-saver.I have a function that I use for Mock API Calls that gets reused a lot.
This little snippet to sleep
I'm afraid to ask where you need this one ππ
I've used it for polling. And... Sometimes you got to do what you got to do π
Hahaha no judgements. Still I let you some polling thingy I had here that may be more suitable:
poll
function is a higher-order function that returns a function,executePoll
.executePoll
function returns apromise
and will run recursively until the condition is met.Args explained:
fn
: an API request (or another async thingy that suits for this polling).validate
: Test function to see if the data matches what we want, in which case it will end the poll.interval
: To specify how much it wait between poll requests.maxAttempts
: how many tries before throwing an error.π
I couldn't sleep last night and read this comment at 3am. Coincidentally, today, I needed this poll. Works great in my usEffect().
The only thing that caught me is that the parameters to the above poll function are in a class, instead of individual parameters.
Thanks for the code!
Didn't get the meaning of the sentence "The only thing that caught me is that the parameters to the above poll function are in a class, instead of individual parameters." still glad to see it helped you π
Apologies, I didn't explain correctly.
Initially, I called the poll function incorrectly as follows:
The correct method (using your code) is:
Oh! nothing to do with classes it's just it receives an object but this is opinionated as I prefer to have the object structure for those "config" thingies, you can simply delete the brackets on the function declaration like that:
and we should be good π
Nice snippet.
The new Promise callback should not be an async function. By passing an async fn to new Promise youβre βdouble promisingβ.
Refactoring to not depend on resolve/reject would solve that.
Or maybe
Having it as async lets you handle the response, the error and any action to perform either it went OK or KO, so it can be customised specifically on it's environment.
instead handling it as generic inside the polling function.
But sure you can tweak it as you wish to fullfill your needs π
This one is pretty neat, I also use it sometimes to "mock" API responses like this π
I love that one!
I've never used that one xD
Love this
I would rather build a reusable library and make it available to anyone that finds it useful.
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.
let callApi=(path) => fetch(path).then(response => response.json())
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.js
now 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:
This is my favourite one and universal too: