Skip to content

Latest commit

 

History

History
191 lines (140 loc) · 4 KB

README.md

File metadata and controls

191 lines (140 loc) · 4 KB

g3kon

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly Semantic Release

g3kon is centralized content store built with Typescript. g3kon is inspired by i18next, but rather than internationalization, g3kon focuses more on centralization

Install

Install with npm:

$ npm install g3kon

Usage example

import { G3kon } from 'g3ckon';

const g3kon = new G3kon({
	contents: {
		general: {
			hello_world: 'Hello World!',
		},
	},
});

console.log(g3kon.g('general.hello_world'));
// => "Hello World!"

You can also have dynamic content with functions!

import { G3kon } from 'g3ckon';

const g3kon = new G3kon({
	contents: {
		general: {
			welcome: (name: string) => `Welcome ${name}!`,
		},
	},
});

console.log(g3kon.g('general.welcome', ['Bob']));
// => "Welcome Bob!"

API

G3kon

Initialize a new G3kon with the given options data.

Params

Example

import { G3kon } from 'g3ckon';

new G3kon({
	contents: {},
});

.g

Get value from key

Params

  • key {String}

  • args {Array}: Arguments for interpolation function, not required if key doesnt map to a non interpolation function

  • returns {String | Number}: Value that key maps to.

Example

const g3kon = new G3kon({
	contents: {
		general: {
			hello_world: 'Hello World!',
			welcome: (name: string) => `Welcome ${name}`,
		},
	},
});

g3kon.g('general.hello_world');
// => "Hello World!"

g3kon.g('general.welcome', ['Bob']);
// => "Welcome Bob!"

.getFixedG

Get value from key

Params

  • (Optional) prefix {String}: Prefix for the key

  • returns {Function}: Get function similar to .g

Example

const g3kon = new G3kon({
	contents: {
		general: {
			hello_world: 'Hello World!',
			welcome: (name: string) => `Welcome ${name}`,
		},
	},
});

// with prefix
const generalG = g3kon.getFixedG('general');

generalG('hello_world');
// => "Hello World!"

generalG('welcome', ['Bob']);
// => "Welcome Bob!"

// without prefix
const g = g3kon.getFixedG('general');

// same as g3kon.g
g('general.hello_world');
// => "Hello World!"

g('general.welcome', ['Bob']);
// => "Welcome Bob!"

Options

Option Type Required Description
contents object true Object from which are keys generated and values retrieved.

Example

const g3kon = new G3kon({
	contents: {
		general: {
			hello_world: 'Hello World!',
			welcome: (name: string) => `Welcome ${name}`,
		},
	},
});

License

Copyright © 2021, Mortynex. Released under the MIT License.