Unverified Commit 93030850 authored by Hong Minhee's avatar Hong Minhee
Browse files

Add `getTypeId()` function

parent a8e1e5e6
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -57,6 +57,8 @@ To be released.
        null` (was `{ name: string; values: Record<string, string> } | null`).
     -  Added `RouterRouteResult` interface.

 -  Added `getTypeId()` function.

 -  Fedify now supports OpenTelemetry for tracing.  [[#170]]

     -  Added `CreateFederationOptions.tracerProvider` option.
+2 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@
 * [`Note` type](https://www.w3.org/TR/activitystreams-vocabulary/#dfn-note)
 * in the Activity Vocabulary.
 *
 * There are two ways to instnatiate an object of a class in this module.
 * There are two ways to instantiate an object of a class in this module.
 * The first way is to use the constructor of the class.  For example:
 *
 * ``` typescript
@@ -52,4 +52,5 @@
export * from "./actor.ts";
export * from "./constants.ts";
export * from "./lookup.ts";
export * from "./type.ts";
export * from "./vocab.ts";

src/vocab/type.test.ts

0 → 100644
+20 −0
Original line number Diff line number Diff line
import { assertEquals } from "@std/assert/assert-equals";
import { test } from "../testing/mod.ts";
import { getTypeId } from "./type.ts";
import { Person } from "./vocab.ts";

test("getTypeId()", () => {
  const obj = new Person({});
  assertEquals(
    getTypeId(obj),
    new URL("https://www.w3.org/ns/activitystreams#Person"),
  );
  const obj2: Person | null = null;
  assertEquals(getTypeId(obj2), null);
  const obj3: Person | undefined = undefined;
  assertEquals(getTypeId(obj3), undefined);
  const obj4: Person | null | undefined = null;
  assertEquals(getTypeId(obj4), null);
  const obj5: Person | null | undefined = undefined;
  assertEquals(getTypeId(obj5), undefined);
});

src/vocab/type.ts

0 → 100644
+99 −0
Original line number Diff line number Diff line
import type { Link, Object } from "./vocab.ts";

/**
 * Returns the type URI of the given object.
 *
 * @example
 * ``` typescript
 * import { getTypeId, Person } from "@fedify/fedify";
 *
 * const obj = new Person({});
 * console.log(getTypeId(obj));
 * // => new URL("https://www.w3.org/ns/activitystreams#Person")
 * ```
 *
 * @param object The Activity Vocabulary object.
 * @returns The type URI of the object, e.g.,
 *          `new URL("https://www.w3.org/ns/activitystreams#Person")`.
 *          If the given `object` is `null` or `undefined`, returns `null` or
 *          `undefined`, respectively.
 * @since 1.3.0
 */
export function getTypeId(object: Object | Link): URL;

/**
 * Returns the type URI of the given object.
 *
 * @example
 * ``` typescript
 * import { getTypeId, Person } from "@fedify/fedify";
 *
 * const obj = new Person({});
 * console.log(getTypeId(obj));
 * // => new URL("https://www.w3.org/ns/activitystreams#Person")
 * ```
 *
 * @param object The Activity Vocabulary object.
 * @returns The type URI of the object, e.g.,
 *          `new URL("https://www.w3.org/ns/activitystreams#Person")`.
 *          If the given `object` is `null` or `undefined`, returns `null` or
 *          `undefined`, respectively.
 * @since 1.3.0
 */
export function getTypeId(object: Object | Link | undefined): URL | undefined;

/**
 * Returns the type URI of the given object.
 *
 * @example
 * ``` typescript
 * import { getTypeId, Person } from "@fedify/fedify";
 *
 * const obj = new Person({});
 * console.log(getTypeId(obj));
 * // => new URL("https://www.w3.org/ns/activitystreams#Person")
 * ```
 *
 * @param object The Activity Vocabulary object.
 * @returns The type URI of the object, e.g.,
 *          `new URL("https://www.w3.org/ns/activitystreams#Person")`.
 *          If the given `object` is `null` or `undefined`, returns `null` or
 *          `undefined`, respectively.
 * @since 1.3.0
 */
export function getTypeId(object: Object | Link | null): URL | null;

/**
 * Returns the type URI of the given object.
 *
 * @example
 * ``` typescript
 * import { getTypeId, Person } from "@fedify/fedify";
 *
 * const obj = new Person({});
 * console.log(getTypeId(obj));
 * // => new URL("https://www.w3.org/ns/activitystreams#Person")
 * ```
 *
 * @param object The Activity Vocabulary object.
 * @returns The type URI of the object, e.g.,
 *          `new URL("https://www.w3.org/ns/activitystreams#Person")`.
 *          If the given `object` is `null` or `undefined`, returns `null` or
 *          `undefined`, respectively.
 * @since 1.3.0
 */
export function getTypeId(
  object: Object | Link | null | undefined,
): URL | null | undefined;

export function getTypeId(
  object: Object | Link | undefined | null,
): URL | undefined | null {
  if (object == null) return object;
  const cls = object.constructor as
    & (new (...args: unknown[]) => Object | Link)
    & {
      typeId: URL;
    };
  return cls.typeId;
}