Skip to content

Commit 5b050e7

Browse files
committed
Use type vars instead of strings for type decls
1 parent 3b13df3 commit 5b050e7

File tree

1 file changed

+88
-52
lines changed

1 file changed

+88
-52
lines changed

jmespath.js

Lines changed: 88 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@
119119
}
120120

121121

122+
// Type constants used to define functions.
123+
var TYPE_NUMBER = 0;
124+
var TYPE_ANY = 1;
125+
var TYPE_STRING = 2;
126+
var TYPE_ARRAY = 3;
127+
var TYPE_OBJECT = 4;
128+
var TYPE_BOOLEAN = 5;
129+
var TYPE_EXPREF = 6;
130+
var TYPE_NULL = 7;
131+
var TYPE_ARRAY_NUMBER = 8;
132+
var TYPE_ARRAY_STRING = 9;
133+
134+
122135
// The "&", "[", "<", ">" tokens
123136
// are not in basicToken because
124137
// there are two token variants
@@ -1139,68 +1152,69 @@
11391152
// types. If the type is "any" then no type checking
11401153
// occurs on the argument. Variadic is optional
11411154
// and if not provided is assumed to be false.
1142-
abs: {func: this.functionAbs, signature: [{types: ["number"]}]},
1143-
avg: {func: this.functionAvg, signature: [{types: ["array-number"]}]},
1144-
ceil: {func: this.functionCeil, signature: [{types: ["number"]}]},
1155+
abs: {func: this.functionAbs, signature: [{types: [TYPE_NUMBER]}]},
1156+
avg: {func: this.functionAvg, signature: [{types: [TYPE_ARRAY_NUMBER]}]},
1157+
ceil: {func: this.functionCeil, signature: [{types: [TYPE_NUMBER]}]},
11451158
contains: {
11461159
func: this.functionContains,
1147-
signature: [{types: ["string", "array"]}, {types: ["any"]}]},
1160+
signature: [{types: [TYPE_STRING, TYPE_ARRAY]},
1161+
{types: [TYPE_ANY]}]},
11481162
"ends_with": {
11491163
func: this.functionEndsWith,
1150-
signature: [{types: ["string"]}, {types: ["string"]}]},
1151-
floor: {func: this.functionFloor, signature: [{types: ["number"]}]},
1164+
signature: [{types: [TYPE_STRING]}, {types: [TYPE_STRING]}]},
1165+
floor: {func: this.functionFloor, signature: [{types: [TYPE_NUMBER]}]},
11521166
length: {
11531167
func: this.functionLength,
1154-
signature: [{types: ["string", "array", "object"]}]},
1168+
signature: [{types: [TYPE_STRING, TYPE_ARRAY, TYPE_OBJECT]}]},
11551169
map: {
11561170
func: this.functionMap,
1157-
signature: [{types: ["expref"]}, {types: ["array"]}]},
1171+
signature: [{types: [TYPE_EXPREF]}, {types: [TYPE_ARRAY]}]},
11581172
max: {
11591173
func: this.functionMax,
1160-
signature: [{types: ["array-number", "array-string"]}]},
1174+
signature: [{types: [TYPE_ARRAY_NUMBER, TYPE_ARRAY_STRING]}]},
11611175
"merge": {
11621176
func: this.functionMerge,
1163-
signature: [{types: ["object"], variadic: true}]
1177+
signature: [{types: [TYPE_OBJECT], variadic: true}]
11641178
},
11651179
"max_by": {
11661180
func: this.functionMaxBy,
1167-
signature: [{types: ["array"]}, {types: ["expref"]}]
1181+
signature: [{types: [TYPE_ARRAY]}, {types: [TYPE_EXPREF]}]
11681182
},
1169-
sum: {func: this.functionSum, signature: [{types: ["array-number"]}]},
1183+
sum: {func: this.functionSum, signature: [{types: [TYPE_ARRAY_NUMBER]}]},
11701184
"starts_with": {
11711185
func: this.functionStartsWith,
1172-
signature: [{types: ["string"]}, {types: ["string"]}]},
1186+
signature: [{types: [TYPE_STRING]}, {types: [TYPE_STRING]}]},
11731187
min: {
11741188
func: this.functionMin,
1175-
signature: [{types: ["array-number", "array-string"]}]},
1189+
signature: [{types: [TYPE_ARRAY_NUMBER, TYPE_ARRAY_STRING]}]},
11761190
"min_by": {
11771191
func: this.functionMinBy,
1178-
signature: [{types: ["array"]}, {types: ["expref"]}]
1192+
signature: [{types: [TYPE_ARRAY]}, {types: [TYPE_EXPREF]}]
11791193
},
1180-
type: {func: this.functionType, signature: [{types: ["any"]}]},
1181-
keys: {func: this.functionKeys, signature: [{types: ["object"]}]},
1182-
values: {func: this.functionValues, signature: [{types: ["object"]}]},
1183-
sort: {func: this.functionSort, signature: [{types: ["array-string", "array-number"]}]},
1194+
type: {func: this.functionType, signature: [{types: [TYPE_ANY]}]},
1195+
keys: {func: this.functionKeys, signature: [{types: [TYPE_OBJECT]}]},
1196+
values: {func: this.functionValues, signature: [{types: [TYPE_OBJECT]}]},
1197+
sort: {func: this.functionSort, signature: [{types: [TYPE_ARRAY_STRING, TYPE_ARRAY_NUMBER]}]},
11841198
"sort_by": {
11851199
func: this.functionSortBy,
1186-
signature: [{types: ["array"]}, {types: ["expref"]}]
1200+
signature: [{types: [TYPE_ARRAY]}, {types: [TYPE_EXPREF]}]
11871201
},
11881202
join: {
11891203
func: this.functionJoin,
11901204
signature: [
1191-
{types: ["string"]},
1192-
{types: ["array-string"]}
1205+
{types: [TYPE_STRING]},
1206+
{types: [TYPE_ARRAY_STRING]}
11931207
]
11941208
},
11951209
reverse: {
11961210
func: this.functionReverse,
1197-
signature: [{types: ["string", "array"]}]},
1198-
"to_array": {func: this.functionToArray, signature: [{types: ["any"]}]},
1199-
"to_string": {func: this.functionToString, signature: [{types: ["any"]}]},
1200-
"to_number": {func: this.functionToNumber, signature: [{types: ["any"]}]},
1211+
signature: [{types: [TYPE_STRING, TYPE_ARRAY]}]},
1212+
"to_array": {func: this.functionToArray, signature: [{types: [TYPE_ANY]}]},
1213+
"to_string": {func: this.functionToString, signature: [{types: [TYPE_ANY]}]},
1214+
"to_number": {func: this.functionToNumber, signature: [{types: [TYPE_ANY]}]},
12011215
"not_null": {
12021216
func: this.functionNotNull,
1203-
signature: [{types: ["any"], variadic: true}]
1217+
signature: [{types: [TYPE_ANY], variadic: true}]
12041218
}
12051219
};
12061220
}
@@ -1259,20 +1273,27 @@
12591273
},
12601274

12611275
typeMatches: function(actual, expected, argValue) {
1262-
if (expected === "any") {
1276+
if (expected === TYPE_ANY) {
12631277
return true;
12641278
}
1265-
if (expected.indexOf("array") === 0) {
1279+
if (expected === TYPE_ARRAY_STRING ||
1280+
expected === TYPE_ARRAY_NUMBER ||
1281+
expected === TYPE_ARRAY) {
12661282
// The expected type can either just be array,
12671283
// or it can require a specific subtype (array of numbers).
12681284
//
12691285
// The simplest case is if "array" with no subtype is specified.
1270-
if (expected === "array") {
1271-
return actual.indexOf("array") === 0;
1272-
} else if (actual.indexOf("array") === 0) {
1286+
if (expected === TYPE_ARRAY) {
1287+
return actual === TYPE_ARRAY;
1288+
} else if (actual === TYPE_ARRAY) {
12731289
// Otherwise we need to check subtypes.
12741290
// I think this has potential to be improved.
1275-
var subtype = expected.split("-")[1];
1291+
var subtype;
1292+
if (expected === TYPE_ARRAY_NUMBER) {
1293+
subtype = TYPE_NUMBER;
1294+
} else if (expected === TYPE_ARRAY_STRING) {
1295+
subtype = TYPE_STRING;
1296+
}
12761297
for (var i = 0; i < argValue.length; i++) {
12771298
if (!this.typeMatches(
12781299
this.getTypeName(argValue[i]), subtype,
@@ -1289,22 +1310,22 @@
12891310
getTypeName: function(obj) {
12901311
switch (Object.prototype.toString.call(obj)) {
12911312
case "[object String]":
1292-
return "string";
1313+
return TYPE_STRING;
12931314
case "[object Number]":
1294-
return "number";
1315+
return TYPE_NUMBER;
12951316
case "[object Array]":
1296-
return "array";
1317+
return TYPE_ARRAY;
12971318
case "[object Boolean]":
1298-
return "boolean";
1319+
return TYPE_BOOLEAN;
12991320
case "[object Null]":
1300-
return "null";
1321+
return TYPE_NULL;
13011322
case "[object Object]":
13021323
// Check if it's an expref. If it has, it's been
13031324
// tagged with a jmespathType attr of 'Expref';
13041325
if (obj.jmespathType === "Expref") {
1305-
return "expref";
1326+
return TYPE_EXPREF;
13061327
} else {
1307-
return "object";
1328+
return TYPE_OBJECT;
13081329
}
13091330
}
13101331
},
@@ -1321,7 +1342,7 @@
13211342

13221343
functionReverse: function(resolvedArgs) {
13231344
var typeName = this.getTypeName(resolvedArgs[0]);
1324-
if (typeName === "string") {
1345+
if (typeName === TYPE_STRING) {
13251346
var originalStr = resolvedArgs[0];
13261347
var reversedStr = "";
13271348
for (var i = originalStr.length - 1; i >= 0; i--) {
@@ -1395,7 +1416,7 @@
13951416
functionMax: function(resolvedArgs) {
13961417
if (resolvedArgs[0].length > 0) {
13971418
var typeName = this.getTypeName(resolvedArgs[0][0]);
1398-
if (typeName === "number") {
1419+
if (typeName === TYPE_NUMBER) {
13991420
return Math.max.apply(Math, resolvedArgs[0]);
14001421
} else {
14011422
var elements = resolvedArgs[0];
@@ -1415,7 +1436,7 @@
14151436
functionMin: function(resolvedArgs) {
14161437
if (resolvedArgs[0].length > 0) {
14171438
var typeName = this.getTypeName(resolvedArgs[0][0]);
1418-
if (typeName === "number") {
1439+
if (typeName === TYPE_NUMBER) {
14191440
return Math.min.apply(Math, resolvedArgs[0]);
14201441
} else {
14211442
var elements = resolvedArgs[0];
@@ -1442,7 +1463,22 @@
14421463
},
14431464

14441465
functionType: function(resolvedArgs) {
1445-
return this.getTypeName(resolvedArgs[0]);
1466+
switch (this.getTypeName(resolvedArgs[0])) {
1467+
case TYPE_NUMBER:
1468+
return "number";
1469+
case TYPE_STRING:
1470+
return "string";
1471+
case TYPE_ARRAY:
1472+
return "array";
1473+
case TYPE_OBJECT:
1474+
return "object";
1475+
case TYPE_BOOLEAN:
1476+
return "boolean";
1477+
case TYPE_EXPREF:
1478+
return "expref";
1479+
case TYPE_NULL:
1480+
return "null";
1481+
}
14461482
},
14471483

14481484
functionKeys: function(resolvedArgs) {
@@ -1466,15 +1502,15 @@
14661502
},
14671503

14681504
functionToArray: function(resolvedArgs) {
1469-
if (this.getTypeName(resolvedArgs[0]) === "array") {
1505+
if (this.getTypeName(resolvedArgs[0]) === TYPE_ARRAY) {
14701506
return resolvedArgs[0];
14711507
} else {
14721508
return [resolvedArgs[0]];
14731509
}
14741510
},
14751511

14761512
functionToString: function(resolvedArgs) {
1477-
if (this.getTypeName(resolvedArgs[0]) === "string") {
1513+
if (this.getTypeName(resolvedArgs[0]) === TYPE_STRING) {
14781514
return resolvedArgs[0];
14791515
} else {
14801516
return JSON.stringify(resolvedArgs[0]);
@@ -1484,9 +1520,9 @@
14841520
functionToNumber: function(resolvedArgs) {
14851521
var typeName = this.getTypeName(resolvedArgs[0]);
14861522
var convertedValue;
1487-
if (typeName === "number") {
1523+
if (typeName === TYPE_NUMBER) {
14881524
return resolvedArgs[0];
1489-
} else if (typeName === "string") {
1525+
} else if (typeName === TYPE_STRING) {
14901526
convertedValue = +resolvedArgs[0];
14911527
if (!isNaN(convertedValue)) {
14921528
return convertedValue;
@@ -1497,7 +1533,7 @@
14971533

14981534
functionNotNull: function(resolvedArgs) {
14991535
for (var i = 0; i < resolvedArgs.length; i++) {
1500-
if (this.getTypeName(resolvedArgs[i]) !== "null") {
1536+
if (this.getTypeName(resolvedArgs[i]) !== TYPE_NULL) {
15011537
return resolvedArgs[i];
15021538
}
15031539
}
@@ -1519,7 +1555,7 @@
15191555
var exprefNode = resolvedArgs[1];
15201556
var requiredType = this.getTypeName(
15211557
interpreter.visit(exprefNode, sortedArray[0]));
1522-
if (["number", "string"].indexOf(requiredType) < 0) {
1558+
if ([TYPE_NUMBER, TYPE_STRING].indexOf(requiredType) < 0) {
15231559
throw new Error("TypeError");
15241560
}
15251561
var that = this;
@@ -1567,7 +1603,7 @@
15671603
functionMaxBy: function(resolvedArgs) {
15681604
var exprefNode = resolvedArgs[1];
15691605
var resolvedArray = resolvedArgs[0];
1570-
var keyFunction = this.createKeyFunction(exprefNode, ["number", "string"]);
1606+
var keyFunction = this.createKeyFunction(exprefNode, [TYPE_NUMBER, TYPE_STRING]);
15711607
var maxNumber = -Infinity;
15721608
var maxRecord;
15731609
var current;
@@ -1584,7 +1620,7 @@
15841620
functionMinBy: function(resolvedArgs) {
15851621
var exprefNode = resolvedArgs[1];
15861622
var resolvedArray = resolvedArgs[0];
1587-
var keyFunction = this.createKeyFunction(exprefNode, ["number", "string"]);
1623+
var keyFunction = this.createKeyFunction(exprefNode, [TYPE_NUMBER, TYPE_STRING]);
15881624
var minNumber = Infinity;
15891625
var minRecord;
15901626
var current;

0 commit comments

Comments
 (0)