build(aio): move the transforms
folder into the tools
folder
This commit is contained in:

committed by
Pete Bacon Darwin

parent
7c418a7e06
commit
abecf3eb6d
93
aio/tools/transforms/examples-package/utils.js
Normal file
93
aio/tools/transforms/examples-package/utils.js
Normal file
@ -0,0 +1,93 @@
|
||||
module.exports = {
|
||||
/**
|
||||
* Transform the values of an object via a mapper function
|
||||
* @param {Object} obj
|
||||
* @param {Function} mapper
|
||||
*/
|
||||
mapObject(obj, mapper) {
|
||||
const mappedObj = {};
|
||||
Object.keys(obj).forEach(key => { mappedObj[key] = mapper(key, obj[key]); });
|
||||
return mappedObj;
|
||||
},
|
||||
|
||||
/**
|
||||
* Parses the attributes from a string taken from an HTML element start tag
|
||||
* E.g. ` a="one" b="two" `
|
||||
* @param {string} str
|
||||
*/
|
||||
parseAttributes(str) {
|
||||
const attrMap = {};
|
||||
let index = 0;
|
||||
|
||||
skipSpace();
|
||||
|
||||
while(index < str.length) {
|
||||
takeAttribute();
|
||||
skipSpace();
|
||||
}
|
||||
|
||||
function takeAttribute() {
|
||||
const key = takeKey();
|
||||
skipSpace();
|
||||
if (tryEquals()) {
|
||||
skipSpace();
|
||||
const quote = tryQuote();
|
||||
attrMap[key] = takeValue(quote);
|
||||
// skip the closing quote or whitespace
|
||||
index++;
|
||||
} else {
|
||||
attrMap[key] = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function skipSpace() {
|
||||
while(index < str.length && /\s/.test(str[index])) {
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
function tryEquals() {
|
||||
if (str[index] === '=') {
|
||||
index++;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function takeKey() {
|
||||
let startIndex = index;
|
||||
while(index < str.length && /[^\s=]/.test(str[index])) {
|
||||
index++;
|
||||
}
|
||||
return str.substring(startIndex, index);
|
||||
}
|
||||
|
||||
function tryQuote() {
|
||||
const quote = str[index];
|
||||
if (['"', '\''].indexOf(quote) !== -1) {
|
||||
index++;
|
||||
return quote;
|
||||
}
|
||||
}
|
||||
|
||||
function takeValue(quote) {
|
||||
let startIndex = index;
|
||||
|
||||
if (quote) {
|
||||
while(index < str.length && str[index] !== quote) {
|
||||
index++;
|
||||
}
|
||||
if (index >= str.length) {
|
||||
throw new Error(`Unterminated quoted attribute value in \`${str}\`. Starting at ${startIndex}. Expected a ${quote} but got "end of string".`);
|
||||
}
|
||||
} else {
|
||||
while(index < str.length && /\S/.test(str[index])) {
|
||||
index++;
|
||||
}
|
||||
}
|
||||
return str.substring(startIndex, index);
|
||||
}
|
||||
|
||||
return attrMap;
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user