Repository URL to install this package:
|
Version:
1.0.0 ▾
|
@truesparrow/common-js
/
context.js
|
|---|
"use strict";
/** Defines {@link Context} and associated helpers. */
Object.defineProperty(exports, "__esModule", { value: true });
/**
* The context of a piece of code. Sometimes you want code to behave differently if
* you're running it in a server (that is, a computer controlled by the company), or on
* a client (that is, a computer controlled by a user), and this enum is useful for that.
*/
var Context;
(function (Context) {
/** Represents the context of code running on a client. */
Context[Context["Client"] = 0] = "Client";
/** Represents the context of code running on a server. */
Context[Context["Server"] = 1] = "Server";
})(Context = exports.Context || (exports.Context = {}));
/**
* Transform a string representation of a context into a {@link Context} value.
* @param context - The string representation of the context.
* @returns A {@link Context} value corresponding to the string representation.
* @throws If the string can't be transformed to a {@link Context} value, throws an {@link Error}.
*/
function parseContext(context) {
if (context === undefined)
throw new Error('Context is not defined');
switch (context.toUpperCase()) {
case "CLIENT":
return Context.Client;
case "SERVER":
return Context.Server;
default:
throw new Error(`Invalid context ${context}`);
}
}
exports.parseContext = parseContext;
/**
* Checks whether a context is the client one or not.
* @param context - The given context.
* @returns A boolean value, indicating whether this is the {@link Context.Client} value or not.
*/
function isClient(context) {
return context == Context.Client;
}
exports.isClient = isClient;
/**
* Checks whether a context is the server one or not.
* @param context - The given context.
* @returns A boolean value, indicating whether this is the {@link Context.Server} value or not.
*/
function isServer(context) {
return context == Context.Server;
}
exports.isServer = isServer;
//# sourceMappingURL=context.js.map