utilium 1.4.0 → 1.5.0

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/dist/objects.d.ts CHANGED
@@ -38,3 +38,14 @@ export type JSONObject = {
38
38
  [K in string]: JSONValue;
39
39
  };
40
40
  export type JSONValue = JSONPrimitive | JSONObject | JSONValue[];
41
+ /**
42
+ * An object `T` with all of its functions bound to a `This` value
43
+ */
44
+ type Bound<T extends object, This = any> = T & {
45
+ [k in keyof T]: T[k] extends (...args: any[]) => any ? (this: This, ...args: Parameters<T[k]>) => ReturnType<T[k]> : T[k];
46
+ };
47
+ /**
48
+ * Binds a this value for all of the functions in an object (not recursive)
49
+ */
50
+ export declare function bindFunctions<T extends object, This = any>(fns: T, thisValue: This): Bound<T, This>;
51
+ export {};
package/dist/objects.js CHANGED
@@ -59,3 +59,9 @@ export function setByString(object, path, value, separator = /[.[\]'"]/) {
59
59
  .filter(p => p)
60
60
  .reduce((o, p, i) => (o[p] = path.split(separator).filter(p => p).length === ++i ? value : o[p] || {}), object);
61
61
  }
62
+ /**
63
+ * Binds a this value for all of the functions in an object (not recursive)
64
+ */
65
+ export function bindFunctions(fns, thisValue) {
66
+ return Object.fromEntries(Object.entries(fns).map(([k, v]) => [k, typeof v == 'function' ? v.bind(thisValue) : v]));
67
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
package/src/objects.ts CHANGED
@@ -118,3 +118,21 @@ export type JSONPrimitive = null | string | number | boolean;
118
118
  export type JSONObject = { [K in string]: JSONValue };
119
119
 
120
120
  export type JSONValue = JSONPrimitive | JSONObject | JSONValue[];
121
+
122
+ /**
123
+ * An object `T` with all of its functions bound to a `This` value
124
+ */
125
+ type Bound<T extends object, This = any> = T & {
126
+ [k in keyof T]: T[k] extends (...args: any[]) => any
127
+ ? (this: This, ...args: Parameters<T[k]>) => ReturnType<T[k]>
128
+ : T[k];
129
+ };
130
+
131
+ /**
132
+ * Binds a this value for all of the functions in an object (not recursive)
133
+ */
134
+ export function bindFunctions<T extends object, This = any>(fns: T, thisValue: This): Bound<T, This> {
135
+ return Object.fromEntries(
136
+ Object.entries(fns).map(([k, v]) => [k, typeof v == 'function' ? v.bind(thisValue) : v])
137
+ ) as Bound<T, This>;
138
+ }