Skip to content

Commit

Permalink
eliminate impossibles
Browse files Browse the repository at this point in the history
  • Loading branch information
mstdokumaci committed Mar 24, 2017
1 parent c333301 commit 8a8ebb2
Showing 1 changed file with 66 additions and 21 deletions.
87 changes: 66 additions & 21 deletions lib/ua/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const andPatch = (and, operator, val) => {

if (operator === '$in' && and.$nin) {
// if you have $in first, $nin can only filter out
var i = and['$nin'].length
let i = and['$nin'].length
while (i--) {
var found = val.indexOf(and['$nin'][i])
if (~found) {
Expand Down Expand Up @@ -228,6 +228,41 @@ const andPatch = (and, operator, val) => {
}
}

// Some impossible combinations to avoid
const impossible = [
{ browser: 'ie', device: 'cast' },
{ browser: 'ie', device: 'firetv' },
{ browser: 'ie', platform: 'android' },
{ browser: 'ie', platform: 'ios' },
{ browser: 'ie', prefix: 'moz' },
{ browser: 'ie', webview: 'ploy-native' },

{ browser: 'edge', device: 'cast' },
{ browser: 'edge', device: 'firetv' },
{ browser: 'edge', platform: 'android' },
{ browser: 'edge', platform: 'ios' },
{ browser: 'edge', prefix: 'moz' },
{ browser: 'edge', webview: 'ploy-native' },

{ browser: 'chrome', prefix: 'ms' },
{ browser: 'chrome', prefix: 'moz' },

{ browser: 'safari', prefix: 'ms' },
{ browser: 'safari', prefix: 'moz' },

{ browser: 'firefox', prefix: 'ms' },
{ browser: 'firefox', prefix: 'webkit' },
{ browser: 'firefox', webview: 'ploy-native' },

{ device: 'bot', platform: 'ios' },
{ device: 'bot', platform: 'android' },
{ device: 'bot', webview: 'ploy-native' },

{ device: 'cast', webview: 'ploy-native' },

{ prefix: 'moz', webview: 'ploy-native' }
]

// Here we merge two objects together by mutating the first one
// { a: 'something' } && { b: 'other thing' } becomes { a: 'something', b: 'other thing' }
const andMerge = (andTo, andFrom, noSimplify) => {
Expand All @@ -245,10 +280,10 @@ const andMerge = (andTo, andFrom, noSimplify) => {

if (andFrom.$or && andFrom.$or.length) {
if (andTo.$or) {
var i = andFrom.$or.length
let i = andFrom.$or.length
while (i--) {
var found = false
var j = andTo.$or.length
let j = andTo.$or.length
while (j--) {
if (JSON.stringify(andFrom.$or[i]) === JSON.stringify(andTo.$or[j])) {
found = true
Expand All @@ -268,6 +303,33 @@ const andMerge = (andTo, andFrom, noSimplify) => {

for (var key in andFrom) {
if (key !== '$or' && key !== '$bin') {
if (andFrom[key].$in) {
let i = impossible.length
while (i--) {
let keys = Object.keys(impossible[i])
let found = keys.indexOf(key)
if (~found) {
keys.splice(found, 1)
let fromIndex = andFrom[key].$in.indexOf(impossible[i][key])
if (~fromIndex) {
let j = keys.length
while (j--) {
if (andTo[keys[j]] && andTo[keys[j]].$in) {
let toIndex = andTo[keys[j]].$in.indexOf(impossible[i][keys[j]])
if (~toIndex) {
andTo[keys[j]].$in.splice(toIndex, 1)
if (!andTo[keys[j]].$in.length) {
andKill(andTo)
return
}
}
}
}
}
}
}
}

if (!andTo[key]) {
andTo[key] = {}
}
Expand Down Expand Up @@ -609,12 +671,6 @@ const generateFakeUA = condition => {
return ua
}

// Some impossible UA combinations to avoid
const rules = [
{ browser: 'ie', platform: 'android' },
{ browser: 'ie', platform: 'ios' }
]

module.exports = ua => {
var start = +new Date()

Expand All @@ -630,6 +686,7 @@ module.exports = ua => {
list.splice(i, 1)
continue
}

list[i] = {
condition: list[i],
ua: JSON.stringify(generateFakeUA(list[i]))
Expand All @@ -645,18 +702,6 @@ module.exports = ua => {
i--
}
}
j = rules.length
while (j--) {
let match = true
for (let key in rules[j]) {
if (list[i].ua[key] !== rules[j][key]) {
match = false
}
}
if (match) {
list.splice(i, 1)
}
}
}

console.log('creating %d builds took %d ms', list.length, +new Date() - start)
Expand Down

0 comments on commit 8a8ebb2

Please sign in to comment.