@artisfy/outputjs
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -5,3 +5,7 @@ import { | ||
} from "./dataObjFromElems"; | ||
import { setLocationKeyValue, getLocationKeyValue } from "./parseNode"; | ||
import { | ||
setLocationKeyValue, | ||
getLocationKeyValue, | ||
setValueForKeyName | ||
} from "./parseNode"; | ||
@@ -70,5 +74,6 @@ // special properties: | ||
setLocationKeyValue, | ||
getLocationKeyValue | ||
getLocationKeyValue, | ||
setValueForKeyName | ||
} | ||
{ | ||
"name": "@artisfy/outputjs", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Recursively crawls the DOM and gathers data from custom attributes", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
import { forEachAttr } from '@artisfy/hummingbird/lib/dom'; | ||
import { dashToCamelCase } from '@artisfy/hummingbird/lib/string'; | ||
import { dashToCamelCase, camelCaseToDash } from '@artisfy/hummingbird/lib/string'; | ||
import { formatSpaces } from "@artisfy/parse-data-attributes"; | ||
@@ -51,3 +51,3 @@ | ||
locationString = formatSpaces(locationString); | ||
let [selector, elemAttribute] = locationString.split(" "); // e.g. ["."] | ||
let [selector, elemAttribute] = locationString.split(" "); // e.g. [".selector", "attr:data-x-text"] | ||
let targetElem; | ||
@@ -79,2 +79,4 @@ | ||
// use like this: setLocationKeyValue(elem, ".selector", "example text") | ||
// ^ this will default to setting innerText if there's no 2nd argument | ||
function setLocationKeyValue (elem, locationString, value) { | ||
@@ -96,2 +98,20 @@ let {elemAttribute, targetElem} = getDataFromLocationString(elem, locationString); | ||
// utility function for setting a value on a data attribute | ||
// using a key name that could be EITHER a location key or an output key | ||
function setValueForKeyName (elem, keyName, value) { | ||
// convert the key name to output and location format | ||
let dashCaseKeyName = camelCaseToDash(keyName) | ||
let outputAttrFormat = "data-o-key-" + dashCaseKeyName; | ||
let locationAttrFormat = "data-l-key-" + dashCaseKeyName; | ||
// if the output format is found, set the value of that attribute | ||
if (elem.hasAttribute(outputAttrFormat)) { | ||
elem.setAttribute(outputAttrFormat, value); | ||
} | ||
// if the location format is found, set the value using `setLocationKeyValue` | ||
if (elem.hasAttribute(locationAttrFormat)) { | ||
let locationString = elem.getAttribute(locationAttrFormat); | ||
setLocationKeyValue(elem, locationString, value); | ||
} | ||
} | ||
export { | ||
@@ -101,3 +121,4 @@ parseNode, | ||
getLocationKeyValue, | ||
setLocationKeyValue | ||
setLocationKeyValue, | ||
setValueForKeyName | ||
}; | ||
@@ -104,0 +125,0 @@ |
8449
175