Add assertNumbers function in ol/style/expressions
This commit is contained in:
committed by
Olivier Guyot
parent
e63bb45e6f
commit
daaf9695ff
@@ -252,6 +252,11 @@ function assertNumber(value) {
|
|||||||
throw new Error(`A numeric value was expected, got ${JSON.stringify(value)} instead`);
|
throw new Error(`A numeric value was expected, got ${JSON.stringify(value)} instead`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
function assertNumbers(arr) {
|
||||||
|
for (let i = 0; i < arr.length; i++) {
|
||||||
|
assertNumber(arr[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
function assertString(value) {
|
function assertString(value) {
|
||||||
if (!(getValueType(value) & ValueTypes.STRING)) {
|
if (!(getValueType(value) & ValueTypes.STRING)) {
|
||||||
throw new Error(`A string value was expected, got ${JSON.stringify(value)} instead`);
|
throw new Error(`A string value was expected, got ${JSON.stringify(value)} instead`);
|
||||||
@@ -350,8 +355,7 @@ Operators['*'] = {
|
|||||||
},
|
},
|
||||||
toGlsl: function(context, args) {
|
toGlsl: function(context, args) {
|
||||||
assertArgsCount(args, 2);
|
assertArgsCount(args, 2);
|
||||||
assertNumber(args[0]);
|
assertNumbers(args);
|
||||||
assertNumber(args[1]);
|
|
||||||
return `(${expressionToGlsl(context, args[0])} * ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} * ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -361,8 +365,7 @@ Operators['/'] = {
|
|||||||
},
|
},
|
||||||
toGlsl: function(context, args) {
|
toGlsl: function(context, args) {
|
||||||
assertArgsCount(args, 2);
|
assertArgsCount(args, 2);
|
||||||
assertNumber(args[0]);
|
assertNumbers(args);
|
||||||
assertNumber(args[1]);
|
|
||||||
return `(${expressionToGlsl(context, args[0])} / ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} / ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -372,8 +375,7 @@ Operators['+'] = {
|
|||||||
},
|
},
|
||||||
toGlsl: function(context, args) {
|
toGlsl: function(context, args) {
|
||||||
assertArgsCount(args, 2);
|
assertArgsCount(args, 2);
|
||||||
assertNumber(args[0]);
|
assertNumbers(args);
|
||||||
assertNumber(args[1]);
|
|
||||||
return `(${expressionToGlsl(context, args[0])} + ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} + ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -383,8 +385,7 @@ Operators['-'] = {
|
|||||||
},
|
},
|
||||||
toGlsl: function(context, args) {
|
toGlsl: function(context, args) {
|
||||||
assertArgsCount(args, 2);
|
assertArgsCount(args, 2);
|
||||||
assertNumber(args[0]);
|
assertNumbers(args);
|
||||||
assertNumber(args[1]);
|
|
||||||
return `(${expressionToGlsl(context, args[0])} - ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} - ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -394,9 +395,7 @@ Operators['clamp'] = {
|
|||||||
},
|
},
|
||||||
toGlsl: function(context, args) {
|
toGlsl: function(context, args) {
|
||||||
assertArgsCount(args, 3);
|
assertArgsCount(args, 3);
|
||||||
assertNumber(args[0]);
|
assertNumbers(args);
|
||||||
assertNumber(args[1]);
|
|
||||||
assertNumber(args[2]);
|
|
||||||
const min = expressionToGlsl(context, args[1]);
|
const min = expressionToGlsl(context, args[1]);
|
||||||
const max = expressionToGlsl(context, args[2]);
|
const max = expressionToGlsl(context, args[2]);
|
||||||
return `clamp(${expressionToGlsl(context, args[0])}, ${min}, ${max})`;
|
return `clamp(${expressionToGlsl(context, args[0])}, ${min}, ${max})`;
|
||||||
@@ -408,8 +407,7 @@ Operators['mod'] = {
|
|||||||
},
|
},
|
||||||
toGlsl: function(context, args) {
|
toGlsl: function(context, args) {
|
||||||
assertArgsCount(args, 2);
|
assertArgsCount(args, 2);
|
||||||
assertNumber(args[0]);
|
assertNumbers(args);
|
||||||
assertNumber(args[1]);
|
|
||||||
return `mod(${expressionToGlsl(context, args[0])}, ${expressionToGlsl(context, args[1])})`;
|
return `mod(${expressionToGlsl(context, args[0])}, ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -419,8 +417,7 @@ Operators['^'] = {
|
|||||||
},
|
},
|
||||||
toGlsl: function(context, args) {
|
toGlsl: function(context, args) {
|
||||||
assertArgsCount(args, 2);
|
assertArgsCount(args, 2);
|
||||||
assertNumber(args[0]);
|
assertNumbers(args);
|
||||||
assertNumber(args[1]);
|
|
||||||
return `pow(${expressionToGlsl(context, args[0])}, ${expressionToGlsl(context, args[1])})`;
|
return `pow(${expressionToGlsl(context, args[0])}, ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -430,8 +427,7 @@ Operators['>'] = {
|
|||||||
},
|
},
|
||||||
toGlsl: function(context, args) {
|
toGlsl: function(context, args) {
|
||||||
assertArgsCount(args, 2);
|
assertArgsCount(args, 2);
|
||||||
assertNumber(args[0]);
|
assertNumbers(args);
|
||||||
assertNumber(args[1]);
|
|
||||||
return `(${expressionToGlsl(context, args[0])} > ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} > ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -441,8 +437,7 @@ Operators['>='] = {
|
|||||||
},
|
},
|
||||||
toGlsl: function(context, args) {
|
toGlsl: function(context, args) {
|
||||||
assertArgsCount(args, 2);
|
assertArgsCount(args, 2);
|
||||||
assertNumber(args[0]);
|
assertNumbers(args);
|
||||||
assertNumber(args[1]);
|
|
||||||
return `(${expressionToGlsl(context, args[0])} >= ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} >= ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -452,8 +447,7 @@ Operators['<'] = {
|
|||||||
},
|
},
|
||||||
toGlsl: function(context, args) {
|
toGlsl: function(context, args) {
|
||||||
assertArgsCount(args, 2);
|
assertArgsCount(args, 2);
|
||||||
assertNumber(args[0]);
|
assertNumbers(args);
|
||||||
assertNumber(args[1]);
|
|
||||||
return `(${expressionToGlsl(context, args[0])} < ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} < ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -463,8 +457,7 @@ Operators['<='] = {
|
|||||||
},
|
},
|
||||||
toGlsl: function(context, args) {
|
toGlsl: function(context, args) {
|
||||||
assertArgsCount(args, 2);
|
assertArgsCount(args, 2);
|
||||||
assertNumber(args[0]);
|
assertNumbers(args);
|
||||||
assertNumber(args[1]);
|
|
||||||
return `(${expressionToGlsl(context, args[0])} <= ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} <= ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -474,8 +467,7 @@ Operators['=='] = {
|
|||||||
},
|
},
|
||||||
toGlsl: function(context, args) {
|
toGlsl: function(context, args) {
|
||||||
assertArgsCount(args, 2);
|
assertArgsCount(args, 2);
|
||||||
assertNumber(args[0]);
|
assertNumbers(args);
|
||||||
assertNumber(args[1]);
|
|
||||||
return `(${expressionToGlsl(context, args[0])} == ${expressionToGlsl(context, args[1])})`;
|
return `(${expressionToGlsl(context, args[0])} == ${expressionToGlsl(context, args[1])})`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -495,9 +487,7 @@ Operators['between'] = {
|
|||||||
},
|
},
|
||||||
toGlsl: function(context, args) {
|
toGlsl: function(context, args) {
|
||||||
assertArgsCount(args, 3);
|
assertArgsCount(args, 3);
|
||||||
assertNumber(args[0]);
|
assertNumbers(args);
|
||||||
assertNumber(args[1]);
|
|
||||||
assertNumber(args[2]);
|
|
||||||
const min = expressionToGlsl(context, args[1]);
|
const min = expressionToGlsl(context, args[1]);
|
||||||
const max = expressionToGlsl(context, args[2]);
|
const max = expressionToGlsl(context, args[2]);
|
||||||
const value = expressionToGlsl(context, args[0]);
|
const value = expressionToGlsl(context, args[0]);
|
||||||
@@ -511,9 +501,7 @@ Operators['array'] = {
|
|||||||
toGlsl: function(context, args) {
|
toGlsl: function(context, args) {
|
||||||
assertArgsMinCount(args, 2);
|
assertArgsMinCount(args, 2);
|
||||||
assertArgsMaxCount(args, 4);
|
assertArgsMaxCount(args, 4);
|
||||||
for (let i = 0; i < args.length; i++) {
|
assertNumbers(args);
|
||||||
assertNumber(args[i]);
|
|
||||||
}
|
|
||||||
const parsedArgs = args.map(function(val) {
|
const parsedArgs = args.map(function(val) {
|
||||||
return expressionToGlsl(context, val, ValueTypes.NUMBER);
|
return expressionToGlsl(context, val, ValueTypes.NUMBER);
|
||||||
});
|
});
|
||||||
@@ -527,9 +515,7 @@ Operators['color'] = {
|
|||||||
toGlsl: function(context, args) {
|
toGlsl: function(context, args) {
|
||||||
assertArgsMinCount(args, 3);
|
assertArgsMinCount(args, 3);
|
||||||
assertArgsMaxCount(args, 4);
|
assertArgsMaxCount(args, 4);
|
||||||
for (let i = 0; i < args.length; i++) {
|
assertNumbers(args);
|
||||||
assertNumber(args[i]);
|
|
||||||
}
|
|
||||||
const array = /** @type {number[]} */(args);
|
const array = /** @type {number[]} */(args);
|
||||||
if (args.length === 3) {
|
if (args.length === 3) {
|
||||||
array.push(1);
|
array.push(1);
|
||||||
|
|||||||
Reference in New Issue
Block a user