UNPKG

@northscaler/service-support

Version:

Artifacts that assist in writing service layers effectively

111 lines (84 loc) 4.78 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: service/extractDtoFromEntity.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: service/extractDtoFromEntity.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>'use strict' const formatDate = require('../formatters/date-formatter') const formatEnumeration = require('../formatters/enumeration-formatter') const DateFormat = require('../enums/DateFormat') const Enumeration = require('@northscaler/enum-support') /** * Extracts a [data transfer object (DTO)](https://martinfowler.com/eaaCatalog/dataTransferObject.html) from the given entity. * The entity normally is an instance of a class with data properties whose names begin with the underscore character (`_`). * By default, this will descend recursively through the given object and drop the leading underscore prefix from its keys and convert any convertible values, mainly `Date`s to ISO-8601 strings and [`Enumeration` instances](https://www.npmjs.com/package/@northscaler/enum-support) to just their `name`s. * All methods found are excluded from the returned DTO. * Each formatter is customizable. * * @param {*} entity The entity from which to extract a data transfer object (DTO). * @param {object} arg1 The argument to be deconstructed. * @param {string|RegExp} [arg1.keyReplacementRegEx=/^_/] The key replacement regular expression given to the `replace` method of `String`; if falsey, no key replacement is performed. * @param {string|function} [arg1.keyReplacement=''] The key replacement given to the `replace` method of `String`. * @param {function} [arg1.dateFormatter] The formatter of `Date`s; defaults to `toISOString()` on `Date`. * @param {function} [arg1.enumerationFormatter] The formatter of [`Enumeration` instances](https://www.npmjs.com/package/@northscaler/enum-support); defaults to returning the `name`. */ function extractDtoFromEntity (entity, { keyReplacementRegEx = /^_/, keyReplacement = '', dateFormatter = date => formatDate({ date, format: DateFormat.ISO_8601 }), enumerationFormatter = enumeration => formatEnumeration({ enumeration, useName: true }) } = {}) { if (keyReplacementRegEx) keyReplacementRegEx = new RegExp(keyReplacementRegEx) function recurse (entity) { // makes the code below more readable return extractDtoFromEntity(entity, { keyReplacementRegEx, keyReplacement, dateFormatter, enumerationFormatter }) } if (!entity) return entity if (Array.isArray(entity)) return entity.map(it => recurse(it)) if (Enumeration.isEnumerationInstance(entity)) return enumerationFormatter(entity) if (typeof entity !== 'object') return entity return Object.keys(entity).reduce((dto, key) => { const newKey = keyReplacementRegEx ? key.replace(keyReplacementRegEx, keyReplacement) : key const type = typeof entity[key] if (!entity[key]) dto[newKey] = entity[key] else if (Array.isArray(entity[key])) dto[newKey] = entity[key].map(it => recurse(it)) else if (entity[key] instanceof Date) dto[newKey] = dateFormatter(entity[key]) else if (Enumeration.isEnumerationInstance(entity[key])) dto[newKey] = enumerationFormatter(entity[key]) else if (type === 'object') dto[newKey] = recurse(entity[key]) else if (type !== 'function') dto[newKey] = entity[key] // skip functions return dto }, {}) } module.exports = extractDtoFromEntity </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#DateFormat">DateFormat</a></li><li><a href="global.html#ResponseStatus">ResponseStatus</a></li><li><a href="global.html#extractDtoFromEntity">extractDtoFromEntity</a></li><li><a href="global.html#formatDate">formatDate</a></li><li><a href="global.html#formatEnumeration">formatEnumeration</a></li><li><a href="global.html#formatError">formatError</a></li><li><a href="global.html#serviceMethod">serviceMethod</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Sun Oct 01 2023 23:45:36 GMT+0000 (Coordinated Universal Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>