npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

loka

v1.0.1

Published

Easily set and access your variables globally without declaring them globally.

Downloads

19

Readme

Loka

Easily make your data accessible throughout the application without declaring them globally.

General features:

  • set: set your variable
  • get: get the variable that you've already set
  • update: update an existing variable
  • delete: delete an existing variable
  • store: access the entire store

Use Case

Lot of times we have to make certain data available throughout the the application, These could be config variables, data we received from the middlewares/database/thirdparty, etc.

Specially in express, If you're doing token based authentication or if your app is multi tenant, You will need access to the user and tenant information throughout the application, And the only way to do it is either by passing the req param to all the methods or using globals which is not recommended, or using a helper module which stores and manages the data.

This is that helper module (unless you're already using one or have created one, in that case you dont need it.) Its simple, readable and gets the job done.

Installation

Let's get started by installing the library, Do npm install loka --save

And then require/import in your project like

import 'Loka' from 'loka';
// or
const Loka = require('loka');

Adding a key to Loka store, Loka.set()

Loka.set('name', 'John Doe');
Loka.set('user', {
	name: 'Johnny Bravo',
    email: '[email protected]',
    gender: 'male',
    verified: true,
});

Accessing a key from Loka store, Loka.get()

const name = Loka.get('name');
console.log(`name: ${name}`); // this will output `name: John Doe`

const user = Loka.get('user');
console.log(user);
// this will output
// {
//     name: 'Johnny Bravo',
//     email: '[email protected]',
//     gender: 'male',
//     verified: true,
// }

Updating a key in Loka store, Loka.update()

Loka.update('name', 'John Doe');
// this will update the key of name `name` in the store and set its value to `John Doe`

const new_name = Loka.update('name', 'Hello World');
console.log(new_name); // this will output the updated value of key `name`, that is `Hello World`

// You can very easily update an object and add new properties, update existing properties of it.
// following our `user` example from adding variables section above, we can do

const updated = Loka.update('user', {
	name: 'Matt Murdock',
	alias: 'Daredevil',
    phone: '111-2222-3333',
});
// this will add and update the properties of the `user` object.

console.log('updated',updated);
// this will output
// updated {
//     name: 'Matt Murdock',
//     email: '[email protected]',
//     gender: 'male',
//     verified: true,
//     alias: 'Daredevil',
//     phone: '111-2222-3333',
// }

Deleting a key in Loka store, Loka.delete()

Loka.delete('name');
// this will delete the key of name `name` in the store.

let user_name = Loka.get('name');
console.log(user_name) // this will output `undefined`

// if an object, you can delete one of its properties instead of the entire object by providing a path
const delete_email = Loka.delete('user', 'email');
console.log(delete_email);
// this will output
// {
//     name: 'Matt Murdock',
//     gender: 'male',
//     verified: true,
//     alias: 'Daredevil',
//     phone: '111-2222-3333',
// }

// or you can just do 

const deleted = Loka.delete('user'); // this will delete key `user` from store along with all of its properties
console.log(deleted);
// this will output `undefined`

Accessing the entire store, Loka.store()

const store = Loka.store();
// this will return the entire store object.

console.log(store);
// this will output `{}` because the store is empty.