Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
mstdokumaci committed Mar 23, 2017
1 parent 6756404 commit 25312fd
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions lib/ua/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ const andPatchTo = (andTo, andFrom, noEliminate) => {
} else if (andFrom.$bin && (andTo.$bin || !Object.keys(andTo).length)) {
andTo.$bin = true
return
} else if (andTo.$bin && !Object.keys(andFrom).length) {
delete andTo.$bin
}

if (andFrom.$or && andFrom.$or.length) {
Expand Down Expand Up @@ -560,12 +562,15 @@ const generateFakeUA = condition => {

while (i--) {
let source = condition[keys[i]] || {}
ua[keys[i]] = {
in: source.$in || [],
nin: source.$nin || [],
min: source['>'] || source['>='] || -Infinity,
max: source['<'] || source['<='] || Infinity
}
let min = source['>'] || source['>='] || -Infinity
let max = source['<'] || source['<='] || Infinity

ua[keys[i]] = source.$in ? source.$in[0]
: source.$nin ? `not-${source.$nin.join('-')}`
: min !== -Infinity && max !== Infinity ? (min + max) / 2
: min !== -Infinity ? +min + 1
: max !== Infinity ? +max - 1
: 'any'
}

return ua
Expand All @@ -586,17 +591,10 @@ module.exports = ua => {
}

list = list.map(condition => {
const ua = generateFakeUA(condition)
for (let key in ua) {
ua[key] = ua[key].in.length ? ua[key].in[0]
: ua[key].nin.length ? `not-${ua[key].nin.join('-')}`
: ua[key].min !== -Infinity && ua[key].max !== Infinity ? (ua[key].min + ua[key].max) / 2
: ua[key].min !== -Infinity ? +ua[key].min + 1
: ua[key].max !== Infinity ? +ua[key].max - 1
: 'any'
return {
condition,
ua: JSON.stringify(generateFakeUA(condition))
}

return { condition, ua: JSON.stringify(ua) }
})

const rules = [
Expand Down

0 comments on commit 25312fd

Please sign in to comment.