@prairielearn/utils 3.1.1 → 3.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/index.js.map +1 -1
- package/dist/index.test.js.map +1 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAkDA,MAAM,UAAU,WAAW,CAAC,KAAY
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAkDA,MAAM,UAAU,WAAW,CAAC,KAAY;IACtC,MAAM,IAAI,KAAK,CAAC,qBAAqB,KAAK,EAAE,CAAC,CAAC;AAChD,CAAC;AAOD;;;;;;;;GAQG;AACH,MAAM,UAAU,SAAS,CACvB,KAAsE;IAEtE,OAAO,KAAU,CAAC;AACpB,CAAC;AA+DD;;;;;GAKG;AACH,MAAM,UAAU,aAAa;IAC3B,IAAI,OAA4C,CAAC;IACjD,IAAI,MAA8B,CAAC;IACnC,MAAM,OAAO,GAAG,IAAI,OAAO,CAAI,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QAC1C,OAAO,GAAG,GAAG,CAAC;QACd,MAAM,GAAG,GAAG,CAAC;IACf,CAAC,CAAC,CAAC;IACH,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAQ,EAAE,MAAM,EAAE,MAAO,EAAE,CAAC;AACzD,CAAC","sourcesContent":["/**\n * Produces a type that only includes the nullable properties of T.\n */\ntype NullableProperties<T> = {\n [K in keyof T as null extends T[K] ? K : never]: T[K];\n};\n\n/**\n * Produces a type containing the keys of T that are nullable.\n */\ntype NullableKeys<T> = keyof NullableProperties<T>;\n\n/**\n * Produces a type with the same keys as T. All properties are marked as required,\n * non-nullable, and not undefined.\n */\ntype RequiredProperty<T> = { [P in keyof T]-?: NonNullable<T[P]> };\n\n/**\n * Produces a type with the same keys as T. If a key is in `RequiredKeys`, it will\n * be marked as non-optional and non-nullable. Otherwise, it will be marked as\n * optional with a type of `undefined`.\n *\n * ```ts\n * type Foo = { a: string; b?: number; c: null };\n * type Bar = WithRequiredKeys<Foo, 'a' | 'b'>;\n * // Bar is equivalent to { a: string; b: number; c?: undefined; }\n * ```\n */\nexport type WithRequiredKeys<T, RequiredKeys extends keyof T> = Omit<\n T,\n RequiredKeys | NullableKeys<T>\n> &\n RequiredProperty<Pick<T, RequiredKeys>> &\n Partial<Record<NullableKeys<Omit<T, RequiredKeys>>, undefined>>;\n\n/**\n * Useful for convincing an IDE to show the expansion of a type.\n *\n * ```ts\n * type Foo = { a: string; b?: number; c: null };\n * type Bar = ExpandRecursively<Foo>;\n * ```\n */\nexport type ExpandRecursively<T> = T extends object\n ? T extends infer O\n ? { [K in keyof O]: ExpandRecursively<O[K]> }\n : never\n : T;\n\nexport function assertNever(value: never): never {\n throw new Error(`Unexpected value: ${value}`);\n}\n\nexport type Result<T, E = Error> = { success: true; value: T } | { success: false; error: E };\n\ndeclare const __brand: unique symbol;\nexport type Brand<K, T> = K & { [__brand]: T };\n\n/**\n * Applies a brand to a value. This is a type-safe identity function that\n * allows you to create branded types from their underlying values.\n *\n * ```ts\n * type UserId = Brand<string, 'UserId'>;\n * const userId = withBrand<UserId>('123'); // userId has type UserId\n * ```\n */\nexport function withBrand<B extends Brand<any, any>>(\n value: B extends Brand<infer K, any> ? Omit<K, typeof __brand> : never,\n): B {\n return value as B;\n}\n\n/**\n * Detects if T is a union type (e.g., 'a' | 'b') vs a single literal (e.g., 'a')\n */\nexport type IsUnion<T, U = T> = T extends unknown ? ([U] extends [T] ? false : true) : never;\n\n/**\n * The Prettify helper is a utility type that takes an object type and makes the hover overlay more readable.\n *\n * https://www.totaltypescript.com/concepts/the-prettify-helper\n */\nexport type Prettify<T> = {\n [K in keyof T]: T[K];\n} & {};\n\n// All keys from any member of union T\ntype UnionKeys<T> = T extends any ? keyof T : never;\n\n// Keys that exist in ALL members of union T\ntype KeysInAllMembers<T> = {\n [K in UnionKeys<T>]: [T] extends [Record<K, any>] ? K : never;\n}[UnionKeys<T>];\n\n// Keys that exist in SOME but not ALL members\ntype KeysInSomeMembers<T> = Exclude<UnionKeys<T>, KeysInAllMembers<T>>;\n\n// Value type for key K across all members that have it\ntype UnionPropValue<T, K extends PropertyKey> = T extends any\n ? K extends keyof T\n ? T[K]\n : never\n : never;\n\n/**\n * Merges a union of object types into a single object type.\n * Keys present in all members become required, keys present in some become optional.\n *\n * ```ts\n * type A = { id: string; name: string };\n * type B = { id: string; age: number };\n * type Merged = MergeUnion<A | B>;\n * // Merged is { id: string } & { name?: string; age?: number }\n * ```\n */\nexport type MergeUnion<T> = {\n [K in KeysInAllMembers<T>]: UnionPropValue<T, K>;\n} & {\n [K in KeysInSomeMembers<T>]?: UnionPropValue<T, K>;\n};\n\n/**\n * An object containing a promise and its resolve/reject methods.\n */\nexport interface PromiseWithResolvers<T> {\n /** The promise instance. */\n promise: Promise<T>;\n /** Resolves the promise. */\n resolve: (value: T | PromiseLike<T>) => void;\n /** Rejects the promise. */\n reject: (reason?: any) => void;\n}\n\n/**\n * Returns an object with a promise and its resolve/reject methods exposed.\n * This is similar to Node.js's util.withResolvers (Node 21+).\n *\n * @returns An object containing the promise, resolve, and reject.\n */\nexport function withResolvers<T>(): PromiseWithResolvers<T> {\n let resolve: (value: T | PromiseLike<T>) => void;\n let reject: (reason?: any) => void;\n const promise = new Promise<T>((res, rej) => {\n resolve = res;\n reject = rej;\n });\n return { promise, resolve: resolve!, reject: reject! };\n}\n"]}
|
package/dist/index.test.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAE9C,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE3C,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE
|
|
1
|
+
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAE9C,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE3C,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;IAC7B,EAAE,CAAC,iCAAiC,EAAE,KAAK,IAAI,EAAE;QAC/C,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,aAAa,EAAU,CAAC;QACrD,UAAU,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC;QACnC,MAAM,MAAM,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC3C,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,iCAAiC,EAAE,KAAK,IAAI,EAAE;QAC/C,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,aAAa,EAAU,CAAC;QACpD,UAAU,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QAChD,MAAM,MAAM,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAChD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { describe, expect, it } from 'vitest';\n\nimport { withResolvers } from './index.js';\n\ndescribe('withResolvers', () => {\n it('resolves with the correct value', async () => {\n const { promise, resolve } = withResolvers<number>();\n setTimeout(() => resolve(123), 10);\n await expect(promise).resolves.toBe(123);\n });\n\n it('rejects with the correct reason', async () => {\n const { promise, reject } = withResolvers<number>();\n setTimeout(() => reject(new Error('fail')), 10);\n await expect(promise).rejects.toThrow('fail');\n });\n});\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prairielearn/utils",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -18,11 +18,11 @@
|
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"@prairielearn/tsconfig": "^2.0.0",
|
|
21
|
-
"@types/node": "^24.
|
|
22
|
-
"@typescript/native-preview": "^7.0.0-dev.
|
|
23
|
-
"@vitest/coverage-v8": "^4.
|
|
21
|
+
"@types/node": "^24.12.2",
|
|
22
|
+
"@typescript/native-preview": "^7.0.0-dev.20260305.1",
|
|
23
|
+
"@vitest/coverage-v8": "^4.1.2",
|
|
24
24
|
"tsx": "^4.21.0",
|
|
25
25
|
"typescript": "^5.9.3",
|
|
26
|
-
"vitest": "^4.
|
|
26
|
+
"vitest": "^4.1.2"
|
|
27
27
|
}
|
|
28
28
|
}
|