Skip to content

Commit 12a36f0

Browse files
jhorbulykandrew-coleman
authored andcommitted
Add typeOf() function. (#381)
* Add typeOf() function. * Add documentation. * Remove .swp file * Fix spacing. * Respond to PR comments: * Rename `typeOf()` to `type()` * Avoid use of Array.includes()
1 parent d5cbecc commit 12a36f0

File tree

16 files changed

+134
-3
lines changed

16 files changed

+134
-3
lines changed

docs/object-functions.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,27 @@ __Examples__
5353
"Postcode: SO21 2JN"
5454
]
5555

56-
## `$error()`
56+
## `$error()`
5757
__Signature:__`$error(message)`
5858

5959
Deliberately throws an error with an optional `message`
6060

61-
## `$assert()`
61+
## `$assert()`
6262
__Signature:__`$assert(condition, message)`
6363

6464
If condition is true, the function returns undefined. If the condition is false, an exception is thrown with the message as the message of the exception.
65+
66+
## `$type()`
67+
__Signature:__`$type(value)`
68+
69+
Evaluates the type of `value` and returns one of the following strings:
70+
* `"null"`
71+
* `"number"`
72+
* `"string"`
73+
* `"boolean"`
74+
* `"array"`
75+
* `"object"`
76+
* `"function"`
77+
Returns (non-string) `undefined` when `value` is `undefined`.
78+
79+

src/functions.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,43 @@ const functions = (() => {
18411841
return undefined;
18421842
}
18431843

1844+
/**
1845+
*
1846+
* @param {*} [value] - the input to which the type will be checked
1847+
* @returns {string} - the type of the input
1848+
*/
1849+
function type(value) {
1850+
if (value === undefined) {
1851+
return undefined;
1852+
}
1853+
1854+
if (value === null) {
1855+
return 'null';
1856+
}
1857+
1858+
if (isNumeric(value)) {
1859+
return 'number';
1860+
}
1861+
1862+
if (typeof value === 'string') {
1863+
return 'string';
1864+
}
1865+
1866+
if (typeof value === 'boolean') {
1867+
return 'boolean';
1868+
}
1869+
1870+
if(Array.isArray(value)) {
1871+
return 'array';
1872+
}
1873+
1874+
if(isFunction(value)) {
1875+
return 'function';
1876+
}
1877+
1878+
return 'object';
1879+
}
1880+
18441881
/**
18451882
* Implements the merge sort (stable) with optional comparator function
18461883
*
@@ -2014,7 +2051,7 @@ const functions = (() => {
20142051
formatNumber, formatBase, number, floor, ceil, round, abs, sqrt, power, random,
20152052
boolean, not,
20162053
map, zip, filter, single, foldLeft, sift,
2017-
keys, lookup, append, exists, spread, merge, reverse, each, error, assert, sort, shuffle, distinct,
2054+
keys, lookup, append, exists, spread, merge, reverse, each, error, assert, type, sort, shuffle, distinct,
20182055
base64encode, base64decode, encodeUrlComponent, encodeUrl, decodeUrlComponent, decodeUrl
20192056
};
20202057
})();

src/jsonata.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,7 @@ var jsonata = (function() {
18591859
staticFrame.bind('each', defineFunction(fn.each, '<o-f:a>'));
18601860
staticFrame.bind('error', defineFunction(fn.error, '<s?:x>'));
18611861
staticFrame.bind('assert', defineFunction(fn.assert, '<bs?:x>'));
1862+
staticFrame.bind('type', defineFunction(fn.type, '<x:s>'));
18621863
staticFrame.bind('sort', defineFunction(fn.sort, '<af?:a>'));
18631864
staticFrame.bind('shuffle', defineFunction(fn.shuffle, '<a:a>'));
18641865
staticFrame.bind('distinct', defineFunction(fn.distinct, '<x:x>'));
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "($var:= undefined; $type($var))",
3+
"dataset": "dataset5",
4+
"bindings": {},
5+
"undefinedResult": true
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "($var:= null; $type($var))",
3+
"dataset": "dataset5",
4+
"bindings": {},
5+
"result": "null"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "($var:= 123; $type($var))",
3+
"dataset": "dataset5",
4+
"bindings": {},
5+
"result": "number"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "($var:= '123'; $type($var))",
3+
"dataset": "dataset5",
4+
"bindings": {},
5+
"result": "string"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "($var:= true; $type($var))",
3+
"dataset": "dataset5",
4+
"bindings": {},
5+
"result": "boolean"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "($var:= 'true'; $type($var))",
3+
"dataset": "dataset5",
4+
"bindings": {},
5+
"result": "string"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"expr": "($var:= []; $type($var))",
3+
"dataset": "dataset5",
4+
"bindings": {},
5+
"result": "array"
6+
}

0 commit comments

Comments
 (0)