[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Utility functions for propWrapperFunctions setting
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | 'use strict';
|
---|
| 6 |
|
---|
| 7 | const filter = require('es-iterator-helpers/Iterator.prototype.filter');
|
---|
| 8 | const some = require('es-iterator-helpers/Iterator.prototype.some');
|
---|
| 9 |
|
---|
| 10 | function searchPropWrapperFunctions(name, propWrapperFunctions) {
|
---|
| 11 | const splitName = name.split('.');
|
---|
| 12 | return some(propWrapperFunctions.values(), (func) => {
|
---|
| 13 | if (splitName.length === 2 && func.object === splitName[0] && func.property === splitName[1]) {
|
---|
| 14 | return true;
|
---|
| 15 | }
|
---|
| 16 | return name === func || func.property === name;
|
---|
| 17 | });
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | function getPropWrapperFunctions(context) {
|
---|
| 21 | return new Set(context.settings.propWrapperFunctions || []);
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | function isPropWrapperFunction(context, name) {
|
---|
| 25 | if (typeof name !== 'string') {
|
---|
| 26 | return false;
|
---|
| 27 | }
|
---|
| 28 | const propWrapperFunctions = getPropWrapperFunctions(context);
|
---|
| 29 | return searchPropWrapperFunctions(name, propWrapperFunctions);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | function getExactPropWrapperFunctions(context) {
|
---|
| 33 | const propWrapperFunctions = getPropWrapperFunctions(context);
|
---|
| 34 | const exactPropWrappers = filter(propWrapperFunctions.values(), (func) => func.exact === true);
|
---|
| 35 | return new Set(exactPropWrappers);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | function isExactPropWrapperFunction(context, name) {
|
---|
| 39 | const exactPropWrappers = getExactPropWrapperFunctions(context);
|
---|
| 40 | return searchPropWrapperFunctions(name, exactPropWrappers);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | function formatPropWrapperFunctions(propWrapperFunctions) {
|
---|
| 44 | return Array.from(propWrapperFunctions, (func) => {
|
---|
| 45 | if (func.object && func.property) {
|
---|
| 46 | return `'${func.object}.${func.property}'`;
|
---|
| 47 | }
|
---|
| 48 | if (func.property) {
|
---|
| 49 | return `'${func.property}'`;
|
---|
| 50 | }
|
---|
| 51 | return `'${func}'`;
|
---|
| 52 | }).join(', ');
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | module.exports = {
|
---|
| 56 | formatPropWrapperFunctions,
|
---|
| 57 | getExactPropWrapperFunctions,
|
---|
| 58 | getPropWrapperFunctions,
|
---|
| 59 | isExactPropWrapperFunction,
|
---|
| 60 | isPropWrapperFunction,
|
---|
| 61 | };
|
---|