0.0.4 • Published 3 months ago

@prostojs/ftring v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

@prostojs/ftring

License: MIT Test Coverage: 100% TypeScript Native

@prostojs/ftring is a lightweight, secure, and efficient JavaScript (TypeScript) library designed to generate functions from strings. This library is particularly useful when pieces of logic need to be configurable and stored in a database for easy modification. It facilitates the dynamic sharing and maintenance of logic, primarily between backend and frontend, allowing for quick checks in the browser while maintaining security and performance.

Rationale

In modern development environments, there is often a need to share logic between the backend and frontend, especially for performing quick checks in the browser. This library allows developers to maintain logic solely on the backend and ship only short strings with checks to the frontend. This ensures that the logic can be dynamically changed and maintained easily without compromising the runtime environment's security.

Security

@prostojs/ftring is built with security considerations in mind. It ensures that no global objects can be accessed from the string function, preventing any potential harm to the runtime.

Installation

npm install @prostojs/ftring

ESM

import { ftring, FtringsPool } from '@prostojs/ftring';

CommonJS

const { ftring, FtringsPool } = require('@prostojs/ftring');

Usage

Basic Usage

import { ftring } from '@prostojs/ftring';

const fn = ftring<number, { a: number; b: number }>('a + b');
console.log(fn({ a: 2, b: 3 })); // Outputs: 5

Using FtringsPool

import { FtringsPool } from '@prostojs/ftring';

const pool = new FtringsPool<number, { a: number; b: number }>();

console.log(pool.call('a + b', { a: 2, b: 3 })); // Outputs: 5

API Reference

ftring<R, CTX>(code: string): ((ctx: CTX) => R)

Generates a new function from the provided string code.

  • R - Return type of the function.
  • CTX - Type of the context object that will be passed to the function.
  • code - String containing the function logic.

FtringsPool<R, CTX>

A class that maintains a pool of ftrings and caches functions so equal code-strings share the same function.

  • call<C extends CTX>(code: string, ctx: C): Calls the function represented by the string code with the provided context.
  • getFn<C extends CTX>(code: string): Retrieves the function represented by the string code.

Use Cases

  1. Configurable Logic: When pieces of logic are configurable and stored in a database, allowing for easy and dynamic configuration changes.
  2. Backend-Frontend Logic Sharing: Sharing a piece of logic stored at the backend with the frontend for quick checks in the browser, enabling dynamic changes and maintenance of logic only on the backend.
  3. Offline Field Checks: For performing offline field checks in the browser, reducing the load on the server and improving user experience by providing instant feedback.

Examples

Dynamic Form Validation

import { ftring } from '@prostojs/ftring';

const validationRule = 'email.includes("@") && password.length >= 8';
const validate = ftring<boolean, { email: string; password: string }>(validationRule);

console.log(validate({ email: 'test@example.com', password: 'password' })); // Outputs: true

Configurable Business Logic

import { FtringsPool } from '@prostojs/ftring';

const businessLogicPool = new FtringsPool<number, { price: number; tax: number }>();

// Assuming logicCode is fetched from a configuration database
const logicCode = 'price + (price * tax / 100)';
console.log(businessLogicPool.call(logicCode, { price: 100, tax: 5 })); // Outputs: 105
0.0.4

3 months ago

0.0.3

7 months ago

0.0.2

7 months ago

0.0.1

7 months ago