Repository
Current version released
2 years ago
Versions
global-js
get, set global values in any js env
Use case
Test
import { setGlobal, getEnv } from '@polioan/global-js'
function toTest() {
return 333
}
if (getEnv('NODE_ENV') === 'test') {
setGlobal('toTest', toTest)
}Global cache (useful for hot reload)
import { getEnv, getGlobal, setGlobal } from '@polioan/global-js'
import { PrismaClient } from '@prisma/client'
export const prisma: PrismaClient = getGlobal('prisma') ?? new PrismaClient({})
if (getEnv('NODE_ENV') !== 'production') {
setGlobal('prisma', prisma)
}Libs for browser
import { setGlobal } from '@polioan/global-js'
class Myjquery {}
setGlobal('$', new Myjquery())Polyfills
import { setGlobal } from '@polioan/global-js'
if (typeof structuredClone === 'undefined') {
setGlobal('structuredClone', value => JSON.parse(JSON.stringify(value)))
}Creating global libraries
import { setGlobal } from '@polioan/global-js'
declare global {
var calculate: (a: number, b: number) => number
}
setGlobal('calculate', (a: number, b: number) => a + b)
const test = calculate(2, 3) // will workInstall
npm i @polioan/global-jsyarn
yarn add @polioan/global-js