/
dimamir
/
node-xml-toolkit
Обзор
Документация
Войти
/
dimamir
/
node-xml-toolkit
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
lib/MoxyLikeJsonEncoder.js
61 строка
1 KB
Dmitry Ovsyanko
MoxyLikeJsonEncoder refactoring, code cleanup, test coverage
05 ноя 2024, 20:10
05 ноя 2024, 20:10
61f5c9d
Код
Авторство
О чём код?
const {TYPES: {CHARACTERS, END_ELEMENT}} = require ('./SAXEvent.js') const MoxyLikeJsonEncoder = function (options = {}) { const getName = options.getName ?? (_ => _) const xform = ({children, attributes}) => { let result = null const set = (newValue, localName, namespaceURI) => { const key = getName (localName, namespaceURI) if (result === null) return result = {[key]: newValue} if (!(key in result)) return result [key] = newValue const oldValue = result [key]; if (!Array.isArray (oldValue)) return result [key] = [oldValue, newValue] oldValue.push (newValue) } for (const [name, value] of attributes.entries ()) set (value, attributes.getLocalName (name), attributes.getNamespaceURI (name)) for (const child of children) switch (child.type) { case CHARACTERS: return child.text case END_ELEMENT: set (xform (child), child.localName, child.namespaceURI) } return result } return function (node) { let result = xform (node) const {wrap, map} = options if (wrap) result = {[getName (node.localName, node.namespaceURI)]: result} if (map) result = map (result) return result } } module.exports = MoxyLikeJsonEncoder