Skip to content

Experiments with Type module #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
remove raw, use Nullable
  • Loading branch information
jmagaram committed Mar 25, 2023
commit 76e9f5b11317f9824773c944c591268a429d536f
157 changes: 84 additions & 73 deletions src/Core__Type.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,243 +2,254 @@

import * as Caml_option from "rescript/lib/es6/caml_option.js";

var isNull = function (a) {
return a === null;
};
function isNull(i) {
return i === null;
}

var isNullOrUndefined = function (a) {
return a === null || a === undefined;
};
function isNullOrUndefined(i) {
if (i === null) {
return true;
} else {
return i === undefined;
}
}

var isUndefined = function (a) {
return a === undefined;
};
function isUndefined(i) {
return i === undefined;
}

function classify(value) {
var match = typeof value;
if (match === "symbol") {
return {
TAG: /* Symbol */ 5,
_0: value,
};
TAG: /* Symbol */5,
_0: value
};
} else if (match === "boolean") {
return {
TAG: /* Bool */ 1,
_0: value,
};
TAG: /* Bool */1,
_0: value
};
} else if (match === "string") {
return {
TAG: /* String */ 4,
_0: value,
};
TAG: /* String */4,
_0: value
};
} else if (match === "function") {
return {
TAG: /* Function */ 6,
_0: value,
};
TAG: /* Function */6,
_0: value
};
} else if (match === "object") {
if (isNull(value)) {
return /* Null */ 1;
if (value === null) {
return /* Null */1;
} else {
return {
TAG: /* Object */ 0,
_0: value,
};
TAG: /* Object */0,
_0: value
};
}
} else if (match === "undefined") {
return /* Undefined */ 0;
return /* Undefined */0;
} else if (match === "number") {
return {
TAG: /* Number */ 2,
_0: value,
};
TAG: /* Number */2,
_0: value
};
} else {
return {
TAG: /* BigInt */ 3,
_0: value,
};
TAG: /* BigInt */3,
_0: value
};
}
}

function toObject(i) {
if (typeof i === "object") {
return Caml_option.some(i);
}

}

function toBool(i) {
if (typeof i === "boolean") {
return i;
}

}

function toFloat(i) {
if (typeof i === "number") {
return i;
}

}

function toBigInt(i) {
if (typeof i === "bigint") {
return Caml_option.some(i);
}

}

function toString(i) {
if (typeof i === "string") {
return i;
}

}

function toSymbol(i) {
if (typeof i === "symbol") {
return Caml_option.some(i);
}

}

function toFunction(i) {
if (typeof i === "function") {
return Caml_option.some(i);
}

}

function getObject(o, n) {
if (isNullOrUndefined(o)) {
return;
return ;
} else {
return toObject(o[n]);
}
}

function getObjectBySymbol(o, s) {
if (isNullOrUndefined(o)) {
return;
return ;
} else {
return toObject(o[s]);
}
}

function getBool(o, n) {
if (isNullOrUndefined(o)) {
return;
return ;
} else {
return toBool(o[n]);
}
}

function getBoolBySymbol(o, s) {
if (isNullOrUndefined(o)) {
return;
return ;
} else {
return toBool(o[s]);
}
}

function getFloat(o, n) {
if (isNullOrUndefined(o)) {
return;
return ;
} else {
return toFloat(o[n]);
}
}

function getFloatBySymbol(o, s) {
if (isNullOrUndefined(o)) {
return;
return ;
} else {
return toFloat(o[s]);
}
}

function getBigInt(o, n) {
if (isNullOrUndefined(o)) {
return;
return ;
} else {
return toBigInt(o[n]);
}
}

function getBigIntBySymbol(o, s) {
if (isNullOrUndefined(o)) {
return;
return ;
} else {
return toBigInt(o[s]);
}
}

function getString(o, n) {
if (isNullOrUndefined(o)) {
return;
return ;
} else {
return toString(o[n]);
}
}

function getStringBySymbol(o, s) {
if (isNullOrUndefined(o)) {
return;
return ;
} else {
return toString(o[s]);
}
}

function getSymbol(o, n) {
if (isNullOrUndefined(o)) {
return;
return ;
} else {
return toSymbol(o[n]);
}
}

function getSymbolBySymbol(o, s) {
if (isNullOrUndefined(o)) {
return;
return ;
} else {
return toSymbol(o[s]);
}
}

function getFunction(o, n) {
if (isNullOrUndefined(o)) {
return;
return ;
} else {
return toFunction(o[n]);
}
}

function getFunctionBySymbol(o, s) {
if (isNullOrUndefined(o)) {
return;
return ;
} else {
return toFunction(o[s]);
}
}

export {
classify,
isUndefined,
isNull,
isNullOrUndefined,
toObject,
toBool,
toFloat,
toBigInt,
toString,
toSymbol,
toFunction,
getObject,
getObjectBySymbol,
getBool,
getBoolBySymbol,
getFloat,
getFloatBySymbol,
getBigInt,
getBigIntBySymbol,
getString,
getStringBySymbol,
getSymbol,
getSymbolBySymbol,
getFunction,
getFunctionBySymbol,
};
classify ,
isUndefined ,
isNull ,
isNullOrUndefined ,
toObject ,
toBool ,
toFloat ,
toBigInt ,
toString ,
toSymbol ,
toFunction ,
getObject ,
getObjectBySymbol ,
getBool ,
getBoolBySymbol ,
getFloat ,
getFloatBySymbol ,
getBigInt ,
getBigIntBySymbol ,
getString ,
getStringBySymbol ,
getSymbol ,
getSymbolBySymbol ,
getFunction ,
getFunctionBySymbol ,
}
/* No side effect */
7 changes: 4 additions & 3 deletions src/Core__Type.res
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ external toStringUnsafe: 'a => string = "%identity"
external toSymbolUnsafe: 'a => Core__Symbol.t = "%identity"
external toFunctionUnsafe: 'a => function = "%identity"

let isNull = %raw(`function(a) { return (a===null); }`)
let isNullOrUndefined = %raw(`function(a) { return (a===null || a===undefined); }`)
let isUndefined = %raw(`function(a) { return (a===undefined); }`)
module N = Core__Nullable
let isNull = i => N.make(i) == N.null
let isNullOrUndefined = i => N.make(i) == N.null || N.make(i) == N.undefined
let isUndefined = i => N.make(i) == N.undefined

let classify = value => {
switch typeof(value) {
Expand Down