Skip to content

Commit

Permalink
Allow JSON Path selectors to work differently if they are 'text'
Browse files Browse the repository at this point in the history
selectors or node selectors. Add 'lowercase' and 'uppercase' mods and
allow them to be applied to arguments.
  • Loading branch information
kickscondor committed Feb 17, 2020
1 parent 92a47e8 commit 8057fc0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
13 changes: 6 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ F.prototype.detect = function (url) {
if (typeof(v) === 'string') {
vars[v] = match[i]
} else if (typeof(v) === 'object') {
vars[v.var] = match[i]
this.assign(vars, {[v.var]: match[i]}, vars, v.mod)
}
}
}
Expand Down Expand Up @@ -123,6 +123,10 @@ F.prototype.assign = function (options, additions, vars, mods) {
val = val.filter(x => x[field] < d)
} else if (trans.startsWith('*')) {
val = val * Number(trans.slice(1))
} else if (trans === 'lowercase') {
val = val.toString().toLowerCase()
} else if (trans === 'uppercase') {
val = val.toString().toUpperCase()
}
}
}
Expand Down Expand Up @@ -261,12 +265,7 @@ F.prototype.scan = async function (vars, site, obj) {
return vars
}
script = site.acceptJson
fn = (path) => {
// let found = JSONPath({path, json: obj})
// return found && found[0]
return jp.value(obj, path)
}

fn = (path, asText) => jp[asText ? 'value' : 'query'](obj, path)
} else if (site.acceptHtml || site.acceptXml) {
if (typeof(obj) === 'string') {
vars.mime = site.acceptHtml ? 'text/html' : 'text/xml'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fraidyscrape",
"description": "Scrape HTML and JSON APIs using JSON definition files.",
"version": "1.0.0",
"version": "1.0.1",
"module": "index.js",
"license": "MIT",
"repository": "kickscondor/fraidyscrape",
Expand Down

0 comments on commit 8057fc0

Please sign in to comment.