4
2
mirror of https://github.com/getbible/app.git synced 2024-06-05 20:10:48 +00:00
app/utils/index.js

58 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-01-08 15:16:47 +00:00
export const isArray = function (a) {
return (!!a) && (a.constructor === Array);
2020-11-10 11:42:16 +00:00
};
2021-01-08 15:16:47 +00:00
export const isObject = function (a) {
return (!!a) && (a.constructor === Object);
2020-11-10 11:42:16 +00:00
};
2021-01-08 15:16:47 +00:00
export const isDate = function (o) {
return o instanceof Object && o.constructor === Date;
}
2020-11-10 11:42:16 +00:00
2021-01-08 15:16:47 +00:00
export const has = function (o, p) {
let has = true
for (const prop of p) {
has = o.hasOwnProperty(prop)
if (!has) break;
}
return has;
2020-11-10 11:42:16 +00:00
}
2021-01-08 15:16:47 +00:00
export const querablePromise = function (promise) {
// Don't modify any promise that has been already modified.
if (promise.isPending) return promise;
// Setup our initial state.
const state = {
isPending: true,
isRejected: false,
isResolved: false,
// Specified so that we'll generate accessor functions for them.
err: undefined,
val: undefined,
};
// We must actually wait for the promise to either resolve or reject,
// wrap that value, then let it continue on.
const result = promise.then(
function (val) {
state.isResolved = true;
state.isPending = false;
state.val = val;
return val;
},
function (err) {
state.isRejected = true;
state.isPending = false;
state.err = err;
throw err;
2020-11-10 11:42:16 +00:00
}
2021-01-08 15:16:47 +00:00
);
for (const val of Object.keys(state)) {
result[val] = function () { return state[val]; };
}
return result;
}