本文整理匯總了TypeScript中src/utils.isObject函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript isObject函數的具體用法?TypeScript isObject怎麽用?TypeScript isObject使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了isObject函數的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: stateUpdated
/**
* Invoked when the state updates.
*
* @param current - The current (most) game state, will be this.next if this.current is undefined.
* @param next - The next (most) game state, will be this.current if this.next is undefined.
* @param delta - The current (most) delta, which explains what happened.
* @param nextDelta - The the next (most) delta, which explains what happend.
*/
public stateUpdated(
current: Immutable<IUnitState>,
next: Immutable<IUnitState>,
delta: Immutable<Delta>,
nextDelta: Immutable<Delta>,
): void {
super.stateUpdated(current, next, delta, nextDelta);
// <<-- Creer-Merge: state-updated -->>
// update the Unit based off its states
this.attackingTile = undefined;
if (nextDelta.type === "ran" && nextDelta.data.run.caller.id === this.id) {
if (nextDelta.data.returned) {
const { run } = nextDelta.data;
const tile = this.game.gameObjects[String(
isObject(run.args.tile) && run.args.tile.id,
)];
switch (run.functionName) {
case "attack":
this.attackingTile = tile && (tile as Tile).getNextMostState();
}
}
}
// <<-- /Creer-Merge: state-updated -->>
}
示例2: deserialize
export function deserialize<T extends unknown>(data: T, game?: BaseGame): T {
if (isObject(data)) {
// TODO: check for game object reference?
const result: UnknownObject = Array.isArray(data) ? [] : {};
for (const key of (Object.keys(data))) {
result[key] = deserialize((data as UnknownObject)[key], game);
}
return result as T;
}
return data;
}
示例3: serialize
export function serialize(
data: unknown,
): string | number | { [key: string]: string | number } {
// Then no need to serialize it, it's already json serializable as a
// string, number, boolean, null, etc.
if (!isObject(data)) {
return data as string | number;
}
if (data.id) {
// No need to serialize this whole game object,
// just send an object reference
return { id: String(data.id) };
}
const serialized: UnknownObject = Array.isArray(data) ? [] : {};
for (const key of Object.keys(data)) {
if (isSerializable(data, key)) {
serialized[key] = serialize(data[key]);
}
}
return serialized as { [key: string]: string | number };
}
示例4: isSerializable
export function isSerializable(obj: object, key: string): boolean {
return isObject(obj) && objectHasProperty(obj, key);
}
示例5: requireGameResource
/** The expected interface all games must export from their interface. */
interface IGameExport {
/** The Namespace that contains all data to initiate a game from. */
namespace: IBaseGameNamespace;
}
// require all images in the build
/** Requires a resource in a game's resources directory. */
export const requireGameResource = require.context("./", true, /^(.*(\.(png|jpe?g)$))[^.]*$/im);
for (const key of requireGameResource.keys()) {
requireGameResource(key);
}
const games: {[gameName: string]: IBaseGameNamespace | undefined} = {};
const req = require.context("./", true, /\.\/([^\/]+)\/index.ts/); // match ./*/index.ts, but only 1 directory.
for (const key of req.keys()) {
const required = req(key) as IGameExport;
if (!required || !isObject(required) || !isObject(required.namespace)) {
throw new Error(`Game "${key}" does not export the expected interface!`);
}
const { namespace } = required;
if (namespace.Game) {
games[namespace.Game.gameName] = namespace;
}
}
/** All the game namespaces that can be loaded by this instance */
export const GAMES = Object.freeze(games);