# [lib.js](source/lib.js) > The backend of function-factory. Author: Anadian Code license: MIT ``` Copyright 2022 Anadian Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ``` Documentation License: [![Creative Commons License](https://i.creativecommons.org/l/by-sa/4.0/88x31.png)](http://creativecommons.org/licenses/by-sa/4.0/) > The source-code comments and documentation are written in [GitHub Flavored Markdown](https://github.github.com/gfm/). ## Functions ### FunctionFactory > Constructor. #### Parametres | name | type | description | | --- | --- | --- | | options | object? | Additional options to pass to the smart constructor. | ##### Options Properties | name | type | description | | --- | --- | --- | | packageMeta | PackageMeta? | An instance of [simple-package-meta](https://github.com/Anadian/simple-package-meta) to be used by this instance and any subclasses initialised along with it. | | logger | object? | The logger to be used by this instance. | | config | ConfigManager? | The [cno-config-manager] instance to be used by the created instance. | | options | object? | The command-line options. | #### Throws | code | type | condition | | --- | --- | --- | | 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if `options` is neither an object nor `null` | #### History | version | change | | --- | --- | | 5.0.0 | Introduced | ### FunctionFactory.load > A static method for initialising a FunctionFactory instance asynchronously while also loading helpers and partials. #### Parametres | name | type | description | | --- | --- | --- | | options | object? | Options; the same as for the FunctionFactory constructor above. \[default: {}\] | #### Returns | type | description | | --- | --- | | Promise | A promise that resolve to a FunctionFactory instance. | #### Throws | code | type | condition | | --- | --- | --- | | 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. | #### History | version | change | | --- | --- | | 5.0.0 | Introduced | ### FunctionFactory.prototype.transform > Transform a given input context into an output string. #### Parametres | name | type | description | | --- | --- | --- | | input_string | string | The input string to transform. | | options | object? | [Reserved] Additional run-time options. \[default: {}\] | #### Returns | type | description | | --- | --- | | Promise | A promise which resolves to the output string. | #### Throws | code | type | condition | | --- | --- | --- | | 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. | #### History | version | change | | --- | --- | | 5.0.0 | Completely rewritten. | ### getNameLiteralFromGenericName > Ensures the given input_string is a generic name and returns a string guaranteed to be a name literal. Parametres: | name | type | description | | --- | --- | --- | | input_string | {string} | The generic name input string to be coverted to a name literal string. | | extension_string | {?string} | An optional string which, if specified, is used as a file extension to be appended to the name literal string if it's not already present. \[default: ''\] | | options | {?Object} | [Reserved] Additional run-time options. \[default: {}\] | Returns: | type | description | | --- | --- | | string | The name literal string. | Throws: | code | type | condition | | --- | --- | --- | | 'ERR_INVALID_ARG_TYPE' | {TypeError} | Thrown if a given argument isn't of the correct type. | Status: | version | change | | --- | --- | | 1.9.0 | Introduced | ### getTemplateFunctionFromFilePath (private) > Returns the template function from the given file path or `null` if not found. Parametres: | name | type | description | | --- | --- | --- | | file_path | {string} | The file path of the [HandleBars](https://handlebarsjs.com/) template to load. | | options | {?Object} | [Reserved] Additional run-time options. \[default: {}\] | Returns: | type | description | | --- | --- | | Promise? | The template function or `null` if it can't be found. | Throws: | code | type | condition | | --- | --- | --- | | 'ERR_INVALID_ARG_TYPE' | {TypeError} | Thrown if a given argument isn't of the correct type. | Status: | version | change | | --- | --- | | 5.0.0 | Massive rebuild | | 1.9.0 | Introduced | ### getTemplateFunctionFromNameLiteral (private) > Returns the template FunctionFactory.prototype.from = function the given name literal string or `null` if none are found. Parametres: | name | type | description | | --- | --- | --- | | name_literal_string | {string} | The name literal as a string. | | options | {?Object} | [Reserved] Additional run-time options. \[default: {}\] | Returns: | type | description | | --- | --- | | {?Function} | The found template FunctionFactory.prototype.or = function `null` otherwise. | Throws: | code | type | condition | | --- | --- | --- | | 'ERR_INVALID_ARG_TYPE' | {TypeError} | Thrown if a given argument isn't of the correct type. | Status: | version | change | | --- | --- | | 1.9.0 | Introduced | ### getTemplateFunctionfromGenericName > Returns the template FunctionFactory.prototype.matching = function the given generic name or `null` if none are found. Parametres: | name | type | description | | --- | --- | --- | | generic_name_string | {string} | The generic name to use to find the template function. | | options | {?Object} | [Reserved] Additional run-time options. \[default: {}\] | Returns: | type | description | | --- | --- | | {?Function} | The template FunctionFactory.prototype.if = function found or `null` otherwise. | Throws: | code | type | condition | | --- | --- | --- | | 'ERR_INVALID_ARG_TYPE' | {TypeError} | Thrown if a given argument isn't of the correct type. | Status: | version | change | | --- | --- | | 1.9.0 | Introduced | ### getTemplateFunctionFromInputContextObject > Returns the template FunctionFactory.prototype.derived = function from either the given input context object or the run-time options object. Parametres: | name | type | description | | --- | --- | --- | | input_context_object | {Object} | The input context object. | | options | {?Object} | Additional run-time options. If a property `template-override` is specified, its value will be used to lookup the template FunctionFactory.prototype.instead = function of the input context object. \[default: {}\] | Returns: | type | description | | --- | --- | | {?Function} | The template FunctionFactory.prototype.if = function found, or `null` otherwise. | Throws: | code | type | condition | | --- | --- | --- | | 'ERR_INVALID_ARG_TYPE' | {TypeError} | Thrown if a given argument isn't of the correct type. | | 'ERR_INVALID_ARG_VALUE' | {Error} | Thrown if no options or properties in the given objects specify a template to lookup. | | 'ERR_INVALID_RETURN_VALUE' | {Error} | Thrown if this FunctionFactory.prototype.receives = function an invalid return value from `getTemplateFunctionFromGenericName` or `getTemplateFunctionFromFilePath`. | Status: | version | change | | --- | --- | | 1.9.0 | Introduced | ### getDefaultInputStringFromFilePath > Returns the default input string after parsing the JSON file at the given path. Parametres: | name | type | description | | --- | --- | --- | | file_path | {string} | The path of the file to read and parse. | | options | {?Object} | [Reserved] Additional run-time options. \[default: {}\] | Returns: | type | description | | --- | --- | | {string} | The file contents as a string. | Throws: | code | type | condition | | --- | --- | --- | | 'ERR_INVALID_ARG_TYPE' | {TypeError} | Thrown if a given argument isn't of the correct type. | Status: | version | change | | --- | --- | | 2.1.1 | Moved to HJSON | | 1.9.0 | Introduced | ### getDefaultInputStringFromNameLiteral > Returns the default input file data as a string or null if there is no default input file found. Parametres: | name | type | description | | --- | --- | --- | | name_literal | {string} | The name literal to lookup the default input for. | | options | {?Object} | [Reserved] Additional run-time options. \[default: {}\] | Returns: | type | description | | --- | --- | | {string} | The default input string or not `null` if a default input file could not be found. | Throws: | code | type | condition | | --- | --- | --- | | 'ERR_INVALID_ARG_TYPE' | {TypeError} | Thrown if a given argument isn't of the correct type. | Status: | version | change | | --- | --- | | 1.9.0 | Introduced | ### getDefaultInputStringFromGenericName > Returns the default input string matching the given template generic name. Parametres: | name | type | description | | --- | --- | --- | | template_generic_name | {string} | The template's generic name to lookup the default input string with. | | options | {?Object} | [Reserved] Additional run-time options. \[default: {}\] | Returns: | type | description | | --- | --- | | {?string} | The default input string, likely in the Resources/defaults directory, matching the given generic name or null if not default-input file is found. | Throws: | code | type | condition | | --- | --- | --- | | 'ERR_INVALID_ARG_TYPE' | {TypeError} | Thrown if a given argument isn't of the correct type. | Status: | version | change | | --- | --- | | 1.9.0 | Introduced |