geton
v0.0.1
Published
A template engine for extracting and transforming data
Maintainers
Readme
geton
A template engine for extracting and transforming data, designed by Shuo Feng.
Table of Contents
Introduction
This lightweight library provides a template engine designed for TypeScript and JavaScript applications, offering customizable capabilities for data extraction and transformation.
Installation
yarn add getonOr
npm install getonUsage
import { get } from "geton";
// Example data with nested objects and arrays
const context = {
user: {
name: "Shuo Feng",
locations: [
{
city: "Beijing",
country: "China",
},
{
city: "Los Angeles",
country: "United States",
},
],
},
};
// Basic property access
console.log("Name:", get("user.name", context));
// Output: "Shuo Feng"
// Access array
console.log("Locations:", get("user.locations", context));
// Output: [{"city":"Beijing","country":"China"},{"city":"Los Angeles","country":"United States"}]
// Access array element by index
console.log("Locations[0]:", get("user.locations.0", context));
// Output: {"city":"Beijing","country":"China"}
// Access nested property in array element
console.log("Locations[0].city:", get("user.locations.0.city", context));
// Output: "Beijing"
// Extract list of specific properties from array
console.log("List of cities:", get("list(user.locations, 'city')", context));
// Output: ["Beijing","Los Angeles"]
// Concatenate array values into a string
console.log(
"String of cities:",
get("concat(list(user.locations, 'city'))", context)
);
// Output: "Beijing,Los Angeles"API Reference
get(template: string, context: TemplateContext, handlers?: HandlerMap): unknown
The core function for extracting and transforming data based on a template string.
| Parameter | Type | Description |
| ---------- | ----------------- | ------------------------------------------------ |
| template | string | A string template with expressions and handlers |
| context | TemplateContext | The data context to extract from |
| handlers | HandlerMap | Optional custom handlers for data transformation |
Default Handlers
| Handler | Description |
| --------------- | ---------------------------------------------- |
| concat | Concatenates multiple values with commas |
| upper | Converts a string to uppercase |
| lower | Converts a string to lowercase |
| replace | Replaces first occurrence of a substring |
| replaceAll | Replaces all occurrences of a substring |
| jsonParse | Parses a JSON string |
| jsonStringify | Stringifies a JSON object |
| list | Extracts a property from each item in an array |
License
This library is MIT licensed.
