Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

kyranet/serialized-tags

Repository files navigation

serialized-tags

NPM version NPM downloads Build status Build Status Total alerts Dependabot Status Patreon

npm installnfo

About

serialized-tags is a fast and lightweight tag template parser for primitive objects and values, it comes with support for Arrays, Objects, Strings, and Value, all four being wrapped in classes implementing the same interface, reducing the need to check values in your code.

The nature of this parser is to allow developers to use language files in the format of their convenience (JSON, YAML...) and pass the output through this to parse the strings and prepare everything for later usage.

This package features a branch for webpack builds for web applications!

Usage

As mentioned before, you can use (parsed) JSON objects for this package, given this file:

{
    "hello": "Hello {0}!",
	"actions.hug": "{0} just hugged {1}!",
	"actions.pat": "{1} just got patted by {0}!",
	"actions.other": [
		"Hello {0}! Came back for more pats?",
		"Bye {0}! I will see you later!"
	]
}

You would parse this using the following code:

// Import the package
const serializedTags = require('serialized-tags');

// Assume jsonString is a string containing the previous JSON
// We will parse it into an object so serializedTags can work with it
const json = JSON.parse(jsonString);

// Parse with serializedTags
const parsed = serializedTags.parse(json);

And now, we save it somewhere! Let's use parsed now!

const salute = parsed.get('hello').display(['world']);
// -> Hello world!

const hug = parsed.get('actions.hug').display(['kyra', 'OSS']);
// -> kyra just hugged OSS!

const pat = parsed.get('actions.pat').display(['the reader', 'kyra']);
// -> kyra just got patted by the reader!

const otherSalute = parsed.get('actions.other').first().display(['kyra']);
// Identical to: parsed.get('actions.other').get(0).display(['kyra']);
// -> Hello kyra! Came back for more pats?

const otherBye = parsed.get('actions.other').last().display(['kyra']);
// Identical to: parsed.get('actions.other').get(1).display(['kyra']);
// -> Bye kyra! I will see you later!

// Or even, randoms!
const otherRandom = parsed.get('actions.other').random().display(['kyra']);
// -> Hello kyra! Came back for more pats?
// -> Bye kyra! I will see you later!

About

Read plain objects and turn plain strings into fast i18n-ready template strings

Resources

License

Stars

Watchers

Forks

Packages

No packages published