mirror of
https://github.com/strapi/strapi.git
synced 2025-07-27 19:10:01 +00:00
40 lines
1.1 KiB
JavaScript
40 lines
1.1 KiB
JavaScript
![]() |
'use strict';
|
||
|
|
||
|
const { curry, isArray, isString, eq, trim, constant } = require('lodash/fp');
|
||
|
|
||
|
const traverseFactory = require('./factory');
|
||
|
|
||
|
const isStringArray = (value) => isArray(value) && value.every(isString);
|
||
|
|
||
|
const fields = traverseFactory()
|
||
|
// Interecept array of strings
|
||
|
.intercept(isStringArray, async (visitor, options, fields, { recurse }) => {
|
||
|
return Promise.all(fields.map((field) => recurse(visitor, options, field)));
|
||
|
})
|
||
|
// Return wildcards as is
|
||
|
.intercept(eq('*'), constant('*'))
|
||
|
// Parse string values
|
||
|
// Since we're parsing strings only, each value should be an attribute name (and it's value, undefined),
|
||
|
// thus it shouldn't be possible to set a new value, and get should return the whole data if key === data
|
||
|
.parse(isString, () => ({
|
||
|
transform: trim,
|
||
|
|
||
|
remove(key, data) {
|
||
|
return data === key ? undefined : data;
|
||
|
},
|
||
|
|
||
|
set(_key, _value, data) {
|
||
|
return data;
|
||
|
},
|
||
|
|
||
|
keys(data) {
|
||
|
return [data];
|
||
|
},
|
||
|
|
||
|
get(key, data) {
|
||
|
return key === data ? data : undefined;
|
||
|
},
|
||
|
}));
|
||
|
|
||
|
module.exports = curry(fields.traverse);
|