UNPKG

@compute.ts/string

Version:

Provide string operators for the computeTS package

406 lines (259 loc) 11.9 kB
![](https://colibri-engine.com/assets/img/compute.png) ![](https://img.shields.io/npm/types/@compute.ts/string) ![](https://img.shields.io/npm/v/@compute.ts/string) ![](https://img.shields.io/npm/l/@compute.ts/string) ![](https://img.shields.io/badge/coverage-98,79%25-4cc41f.svg) ![](https://img.shields.io/badge/files-5-orange) ![](https://img.shields.io/badge/lines-323-orange) ![](https://img.shields.io/badge/made%20in-France-blue) ## Presentation The engine is based on incremental computation algorithms. When a calculation is submitted to the engine, all the computed values are memorized. So, if you change some variable and query an evaluation, the engine is able to compute the result very fast because it recomputes only what has changed. [compute.ts](https://www.npmjs.com/package/compute.ts) ## Libraries The project provides several libraries that each comes with dozens of operators. Please note the cross-compatibility of the libraries. - [@compute.ts/boolean](https://www.npmjs.com/package/@compute.ts/boolean) - [@compute.ts/number](https://www.npmjs.com/package/@compute.ts/number) - [@compute.ts/date](https://www.npmjs.com/package/@compute.ts/date) - [@compute.ts/dictionary](https://www.npmjs.com/package/@compute.ts/dictionary) - [@compute.ts/function](https://www.npmjs.com/package/@compute.ts/function) - [@compute.ts/array](https://www.npmjs.com/package/@compute.ts/array) - [@compute.ts/math](https://www.npmjs.com/package/@compute.ts/math) - [@compute.ts/structural](https://www.npmjs.com/package/@compute.ts/structural) ## Imports ### Typescript ```typescript import {/* required operators */} from '@compute.ts/string'; ``` ### Javascript ```javascript const {/* required operators */} = require('@compute.ts/string'); ``` ## Operators ### string <code>string(value) ➜ x<sub>string</sub></code> <code>string() ➜ x<sub>string</sub></code> The **string** operator allows you to create a string expression which evals to the given value. If no value is provided the expression is a variable of the model ```typescript import {string} from "@compute.ts/string" const x = string('Hello world') | string(); x.affect('World Hello'); ``` * * * ### is <code>is(x<sub>string</sub>, y<sub>string</sub>) ➜ z<sub>boolean</sub></code> <code>x<sub>string</sub>.is(y<sub>string</sub>) ➜ z<sub>boolean</sub></code> The **is** operator allows to create a boolean expression which evals to `true` if x equals y ```typescript import {string, is} from "@compute.ts/string" const x = string(); const y = string(); const z = is(x, y) | x.is(y); const value = z.eval(); ``` * * * ### isNot <code>isNot(x<sub>string</sub>, y<sub>string</sub>) ➜ z<sub>boolean</sub></code> <code>x<sub>string</sub>.isNot(y<sub>string</sub>) ➜ z<sub>boolean</sub></code> The **isNot** operator allows to create a boolean expression which evals to `true` if x is not y ```typescript import {string, isNot} from "@compute.ts/string" const x = string(); const y = string(); const z = isNot(x, y) | x.isNot(y); const value = z.eval(); ``` * * * ### chatAt <code>charAt(x<sub>string</sub>, y<sub>number</sub>) ➜ z<sub>string</sub></code> <code>x<sub>string</sub>.charAt(y<sub>number</sub>) ➜ z<sub>string</sub></code> The **charAt** operator allows to create a string expression which evals to the character fo x at the y position ```typescript import {number} from "@compute.ts/number"; import {string, charAt} from "@compute.ts/string" const x = string(); const y = number(); const z = charAt(x, y) | x.charAt(y); const value = z.eval(); ``` * * * ### indexOf <code>indexOf(x<sub>string</sub>, y<sub>string</sub>) ➜ z<sub>number</sub></code> <code>x<sub>string</sub>.indexOf(y<sub>string</sub>) ➜ z<sub>number</sub></code> The **indexOf** operator allows to create a number expression which evals to the position of y in x ```typescript import {string, indexOf} from "@compute.ts/string" const x = string(); const y = string(); const z = indexOf(x, y) | x.indexOf(y) const value = z.eval(); ``` * * * ### concat <code>concat(x<sup>0</sup><sub>string</sub>, x<sup>1</sup><sub>string</sub>, ..., x<sup>n</sup><sub>string</sub>) ➜ y<sub>string</sub></code> <code>x<sup>0</sup><sub>string</sub>.concat(x<sup>1</sup><sub>string</sub>, ..., x<sup>n</sup><sub>string</sub>) ➜ y<sub>boolean</sub></code> The **concat** operator allows to create a string expression which evals to the concatenation of the given expressions ```typescript import {string, concat} from "@compute.ts/string" const x0 = string(); const x1 = string(); // ... const xn = string(); const y = concat(x0, x1, ..., xn) | x0.concat(x1, ..., xn); const value = y.eval(); ``` * * * ### include <code>include(x<sub>string</sub>, y<sub>string</sub>) ➜ z<sub>boolean</sub></code> <code>x<sub>string</sub>.include(y<sub>string</sub>) ➜ z<sub>boolean</sub></code> The **include** operator allows to create a boolean expression which evals to `true` if x includes y ```typescript import {string, include} from "@compute.ts/string" const x = string(); const y = string(); const z = include(x, y) | x.include(y); const value = z.eval(); ``` * * * ### startsWith <code>startsWith(x<sub>string</sub>, y<sub>string</sub>) ➜ z<sub>boolean</sub></code> <code>x<sub>string</sub>.startsWith(y<sub>string</sub>) ➜ z<sub>boolean</sub></code> The **startsWith** operator allows to create a boolean expression which evals to `true` if x starts with y ```typescript import {string, startsWith} from "@compute.ts/string" const x = string(); const y = string(); const z = startsWith(x, y) | x.startsWith(y); const value = z.eval(); ``` * * * ### endsWith <code>endsWith(x<sub>string</sub>, y<sub>string</sub>) ➜ z<sub>boolean</sub></code> <code>x<sub>string</sub>.endsWith(y<sub>string</sub>) ➜ z<sub>boolean</sub></code> The **endsWith** operator allows to create a boolean expression which evals to `true` if x ends with y ```typescript import {string, endsWith} from "@compute.ts/string" const x = string(); const y = string(); const z = endsWith(x, y) | x.endsWith(y); const value = z.eval(); ``` * * * ### padStart <code>padStart(x<sub>string</sub>, y<sup>1</sup><sub>string</sub>, y<sup>2</sup><sub>number</sub>) ➜ z<sub>string</sub></code> <code>x<sub>string</sub>.padStart(y<sup>1</sup><sub>string</sub>, y<sup>2</sup><sub>number</sub>) ➜ z<sub>string</sub></code> The **start** operator allows to create a string expression which evals to x with y1 repetated by the start until the size reach y2 ```typescript import {string, include} from "@compute.ts/string" import {number} from "@compute.ts/number"; const x = string(); const y1 = string(); const y2 = number(); const z = padStart(x, y1, y2) | x.padStart(y1, y2); const value = z.eval(); ``` * * * ### padEnd <code>padEnd(x<sub>string</sub>, y<sup>1</sup><sub>string</sub>, y<sup>2</sup><sub>number</sub>) ➜ z<sub>string</sub></code> <code>x<sub>string</sub>.padEnd(y<sup>1</sup><sub>string</sub>, y<sup>2</sup><sub>number</sub>) ➜ z<sub>string</sub></code> The **padEnd** operator allows to create a string expression which evals to x with y1 repetated by the end until the size reach y2 ```typescript import {string, include} from "@compute.ts/string" import {number} from "@compute.ts/number"; const x = string(); const y1 = string(); const y2 = number(); const z = padEnd(x, y1, y2) | x.padEnd(y1, y2); const value = z.eval(); ``` * * * ### length <code>length(x<sub>string</sub>) ➜ y<sub>number</sub></code> <code>x<sub>string</sub>.length() ➜ y<sub>number</sub></code> The **length** operator allows to create a number expression which evals to the length of x ```typescript import {string, length} from "@compute.ts/string" const x = string(); const y = length(x) | x.length(); const value = y.eval(); ``` * * * ### trim <code>trim(x<sub>string</sub>) ➜ y<sub>string</sub></code> <code>x<sub>string</sub>.trim() ➜ y<sub>string</sub></code> The **trim** operator allows to create a string expression which evals to x where the left and right spaces has been removed ```typescript import {string, trim} from "@compute.ts/string" const x = string(); const y = trim(x) | x.trim(); const value = y.eval(); ``` * * * ### replace <code>replace(x<sub>string</sub>, y<sup>1</sup><sub>string</sub>, y2string) ➜ z<sub>string</sub></code> <code>x<sub>string</sub>.replace(y<sup>1</sup><sub>string</sub>, y2string) ➜ z<sub>string</sub></code> The **replace** operator allows to create a string expression which evals to x where y1 has been replaced by y2 ```typescript import {string, replace} from "@compute.ts/string" const x = string(); const y1 = string(); const y2 = string(); const z = replace(x, y1, y2) | x.replace(y1, y2); const value = z.eval(); ``` * * * ### subString <code>subString(x<sub>string</sub>, y<sup>1</sup><sub>number</sub>, y<sup>2</sup><sub>number</sub>) ➜ z<sub>string</sub></code> <code>x<sub>string</sub>.subString(y<sup>1</sup><sub>number</sub>, y<sup>2</sup><sub>number</sub>) ➜ z<sub>string</sub></code> The **slice** operator allows to create a string expression which evals to a substring of x where the starting index is y1 and the length is y2 ```typescript import {number} from "@compute.ts/number"; import {string, subString} from "@compute.ts/string" const x = string(); const y1 = number(); const y2 = number(); const y = subString(x, y1, y2) | x.subString(y1, y2); const value = y.eval(); ``` * * * ### slice <code>slice(x<sub>string</sub>, y<sup>1</sup><sub>number</sub>, y<sup>2</sup><sub>number</sub>) ➜ z<sub>string</sub></code> <code>x<sub>string</sub>.slice(y<sup>1</sup><sub>number</sub>, y<sup>2</sup><sub>number</sub>) ➜ z<sub>string</sub></code> The **slice** operator allows to create a string expression which evals to a substring of x where the starting index is y1 and the end is y2 ```typescript import {number} from "@compute.ts/number"; import {string, slice} from "@compute.ts/string" const x = string(); const y1 = number(); const y2 = number(); const y = slice(x, y1, y2) | x.slice(y1, y2); const value = y.eval(); ``` * * * ### toUppercase <code>toUppercase(x<sub>string</sub>) ➜ y<sub>string</sub></code> <code>x<sub>string</sub>.toUppercase() ➜ y<sub>string</sub></code> The **toLowercase** operator allows to create a string expression which evals to x uppercased ```typescript import {string, toUppercase} from "@compute.ts/string" const x = string(); const y = toUppercase(x) | x.toUppercase(); const value = y.eval(); ``` * * * ### toLowercase <code>toLowercase(x<sub>string</sub>) ➜ y<sub>string</sub></code> <code>x<sub>string</sub>.toLowercase() ➜ y<sub>string</sub></code> The **toLowercase** operator allows to create a string expression which evals to x lowercased ```typescript import {string, toLowercase} from "@compute.ts/string" const x = string(); const y = toLowercase(x) | x.toLowercase(); const value = y.eval(); ``` ## About the author I am a software developer with 4 years of project specializing in the development of web solutions. Digital Nomad, I work while traveling. After 3 years into the french industry, I've started to work as a freelance software architect or fullstack developer. Based on state-of-the-art web technologies, I offer you to architect & develop the best version of your project. My experience in the web app development assure you to build a nice looking, performant and stable product. Minimalist, I like to travel, to meet people, learn new things and handmade stuff. Scuba & sky diving licenced. I like also hamburgers, Kinder chocolate and crepes. Karate black belt. https://berthellemy.com/ * * * ![](https://colibri-engine.com/assets/img/brand/logo_animated.gif)