Getting started...
Installing package using npm
npm i @marthvon/protopp
First time using npm? No worries...
Run the following to create a package.json within the directory before calling install.
npm init
Setup prototypes in main.js or index.js
In the root file, usually referring to the entrypoint of the application often following the naming conventions of index.js, main.js, or app.js, calling import here once will add all the prototype extensions. And, any consequent call to other files will share the same modified prototypes allowing you to use the additional methods implemented to the listed native JS types as you would like any other method.
index.js
import '@marthvon/protopp';
Optionally, just import the methods you need to reduce initial overhead at the beginning. My advice is put all the prototype imports into a separate file and import that single file on the js script entrypoint.
MyCustomPrototypes.js
import '@marthvon/protopp/objectpp/methods/copyOnly.js';
import '@marthvon/protopp/objectpp/methods/removeKey.js';
import '@marthvon/protopp/arraypp/methods/searchSorted.js';
import '@marthvon/protopp/stringpp/methods/sanitizeRegex.js';
import '@marthvon/protopp/stringpp/methods/snakeToTitleCase.js';
// etc...
Server, Client & Cloud environments
It's important to note that importing the package at the web client's root entrypoint will modify the prototype at the client, BUT!!! it wouldn't modify server-side prototypes. To use prototypes on the server, import method must be called at the root entrypoint of the server.
In most frameworks, it would be as simple as adding it on the app.js of your respective framework. But for frameworks like, nextjs or svelte, it doesn't require common boilerplate code like app.js defining the App instance, or defining things like an app router. It may be confusing where to put the import statement given the following mentioned cases.
Often for client side code, adding it in the root layout element solves the issue. As for server-side it get's a little tricky, one advice I can provide is to add the import statement in the root middleware. Naturally, the issue can be resolve by just importing the method every time it is used in a file context. There is no additional overhead as the prototype methods are guarded to create and be assigned only once regardless of how many times the file is imported. When hosting in cloud environments, the latter solution may be required as it's uncertain if the virtualized environment of a cloud server would use the same modified prototypes imported by another process.
When encountering errors for undefined method
// NOTE: this is entirely asynchronous
import '@marthvon/protopp';
Ideally, the prototype extension methods are updated by the time the application runs the code using the extension method.
// When if it fails, use await import('...')
await import('...');
Although, the idea is to not block the main js thread, but in a big application like react project or something - the assumption is that the updating of the prototypes happens instantaneous while react setups.