FCF 2.0 development in progress...
> > > >
[News] [Packages API] [Downloads] [Donate to the project] [Contacts]

fcf.tokenize() function

mixed fcf.tokenize(strign|object a_code, object a_environment = {}, object a_options = {quiet: false, result: "string"})

Package: fcf-framework-core

File: fcf-framework-core:fcf.js

Available from version: 2.0.2

Performs tokenization of the given string. The control constructs of the tokenizer are @{{...}}@ and !{{...}}!.

When tokenizing the input string, control constructs are expanded.

  • Single line Javascript command control structure

    This type of computed construct contains a single-line Javascript command placed in an @{{...}}@ block. Inside this block, only single-line JS operations are allowed, which do not write data and cannot lead to execution hang, i.e. select operation ( CMP ? ARG1 : ARG2 ) and arithmetic operations are allowed. Also, only safe functions that do not write data are available. Access to global variables is implemented only for constants from Math and fcf objects. This approach is implemented only on the server side; on the browser side, the usual eval() call is performed.

    How to add permission to execute additional functions and methods is described on the tokenization mechanism description page: tokenization.

  • Translation insertion construct

    This construction translates a substring enclosed in a !{{...}}! block. Substring translation is carried out by system means of the framework (function fcf.t()). If the translation for the given substring is not found, then the original substring from the !{{...}}! construction is returned to the resulting string.

    Inside the !{{...}}! it is allowed to place the @{{...}}@ construction, which allows to form translatable strings with changing data.

Arguments

string|object a_code
- The string to be tokenized, or the object that will be tokenized for all nested values that are strings.

object a_environment = {}
- An object containing variables passed to the tokenizer

object a_options = {quiet: false, result: "string"}
- Additional options object:

  • boolean quiet = false - If the argument is false then an error is thrown exception.

    If the argument is true, then the @{{...}}@ constructs in which an error occurred are passed to the resulting string without calculation of the result (the @{{...}}@ construct is passed as it was passed to the function).

  • string" result = "string" - If the option is "string", then the function always returns a string as its result.

    If the option is "auto", then if the input string contains only the @{{...}}@ construct, without additional characters, the function will return the original result of the @{{...}}@ construct evaluation, if the input string contains additional characters, then the function will return a string representation.

Result
mixed
- Result of tokenization

Example: Application of the design structure

let result = fcf.tokenize("Next value: @{{value + 1}}@", {value: 1}); console.log(result);

Output:

Next value: 2

Example: Returning the original value

let result = fcf.tokenize("@{{array[0]}}@", {array: [{value: 1}]}, {result: "auto"}); console.log(result);

Output:

{ value: 1 }

Example: Using translation

fcf.getConfiguration().append({ translations: [ { language: "ja", translations: { "Next value: @{{value + 1}}@": "次の値: @{{value + 1}}@", } } ] }); fcf.getContext().language = "ja"; let result = fcf.tokenize("Text: !{{Next value: @{{value + 1}}@}}!", {value: 1}); console.log(result);

Output:

Text: 次の値: 2