@nathanaelscott/madlibs
Advanced tools
Comparing version 1.0.2 to 1.1.0
25
index.js
@@ -38,6 +38,18 @@ const ac2o = require("alternating-case-to-object"); | ||
const getAnchorOptions = (target)=>{ | ||
if(target.title) return target.title; | ||
if(target.getAttribute('href')){ | ||
let href = target.getAttribute('href'); | ||
if(href.indexOf('#')<0 || href.length<=1) return false; | ||
return href.substring(1); | ||
} | ||
return false; | ||
} | ||
//Constructor | ||
const MadLibs = options => { | ||
//Get words on page | ||
let targets = [...document.querySelectorAll('a[title^="WORD"]')]; | ||
let targets = [...document.querySelectorAll('a[title^="WORD"], a[href^="#WORD"]')]; | ||
@@ -47,3 +59,8 @@ targets.forEach(target => { | ||
try { | ||
let wordOpt = ac2o(target.title); | ||
let wordHash = getAnchorOptions(target); | ||
if(!wordHash) throw 'No valid hash found'; | ||
let wordOpt = ac2o(wordHash); | ||
let wordData = options[wordOpt.word]; | ||
@@ -66,4 +83,4 @@ let word; | ||
replaceSingleWord(target, word); | ||
} catch (e) { | ||
console.error('[MadLibs]',target,e); | ||
} catch (err) { | ||
console.error('[MadLibs]',target,err); | ||
//Remove the el anyway; | ||
@@ -70,0 +87,0 @@ replaceEl(target, "span"); |
{ | ||
"name": "@nathanaelscott/madlibs", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "A component for replacing words in the dom from a JSON file", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
102
README.md
@@ -21,2 +21,3 @@ # Mad Libs | ||
'flowers':{ | ||
'type':'nested', | ||
'array':[ | ||
@@ -26,4 +27,13 @@ {state:'VIC',flower:'Epacris impressa',size:'150mm'}, | ||
{state:'TAS',flower:'Tasmanian blue gum',size:'95mm'} | ||
], | ||
'type':'nested' | ||
] | ||
}, | ||
'animals':{ | ||
'type':'variable', | ||
'biggest':'elephant', | ||
'smallest':'shrew' | ||
}, | ||
'me':{ | ||
'type':'single', | ||
'word':'Nathanael Scott' | ||
} | ||
@@ -38,5 +48,89 @@ } | ||
Accepts the camel cased string in the title attribute or with a leading # in the href attribute. | ||
```html | ||
<a name="WORDflowersRANK1VALUEflower"></a> <!-- Returns: Epacris imperessa--> | ||
<a name="WORDflowersSTATEtasVALUEsize"></a> <!-- Returns: 95mm--> | ||
<a title="WORDflowersRANK1VALUEflower"></a> <!-- Returns: Epacris imperessa --> | ||
<a title="WORDflowersSTATEtasVALUEsize"></a> <!-- Returns: 95mm --> | ||
<a href="#WORDanimalsVALUEsmallest"></a> <!-- Returns: shrew --> | ||
<a href="#WORDme"></a> <!-- Returns: Nathanael Scott --> | ||
``` | ||
Returns: | ||
```html | ||
<span class="madlib-word">Epacris imperessa</span> | ||
<span class="madlib-word">95mm</span> | ||
<span class="madlib-word">shrew</span> | ||
<span class="madlib-word">Nathanael Scott</span> | ||
``` | ||
## Options | ||
Mad Libs accepts a nested object. The object key is word the ID. | ||
```javascript | ||
const options = { | ||
'flowers':{...} | ||
} | ||
``` | ||
Adding camal cased option to the name attribute starting with WORD followed by the ID replaces the element with a span containg the output | ||
```html | ||
<a name="WORDflowers"></a> | ||
``` | ||
# type | ||
nested | ||
Returns values from an array of objects using it's rank (position in array) or id | ||
```javascript | ||
'flowers':{ | ||
'type':'nested', | ||
'array':[ | ||
{state:'VIC',flower:'Epacris impressa',size:'150mm'}, | ||
{state:'NSW',flower:'Waratah',size:'113mm'}, | ||
{state:'TAS',flower:'Tasmanian blue gum',size:'95mm'} | ||
] | ||
} | ||
``` | ||
```html | ||
<a name="WORDflowersRANK1VALUEflower"></a> | ||
<!-- Returns the value flower from first entry in the array --> | ||
<a name="WORDflowersSTATEtasVALUEsize"></a> | ||
<!-- Returns the value size from the entry with the state TAS --> | ||
<a name="WORDflowersRANK2VALUEstate"></a> | ||
<!-- Returns the long Australian state name from the 2nd entry in the array --> | ||
``` | ||
Options accepts | ||
array - An ordered array (ordered to your own critera) with either a state (uppercase appreviation) or type variable as identification | ||
Anchor options | ||
state, type | ||
rank | ||
value | ||
variable | ||
Get values from the object via their names | ||
Anchor options | ||
value | ||
single | ||
Return a single word from the word value | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6309
94
134