@tidyjs/tidy 2.4.5 → 2.4.6

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.
@@ -1 +1 @@
1
- {"version":3,"file":"first.js","sources":["../../../src/summary/first.ts"],"sourcesContent":["/**\n * Returns a function that returns the first value for the specified key\n * @param key A string key of the object or an accessor converting the object to a number\n */\nexport function first<T extends object>(key: keyof T | ((d: T) => number)) {\n const keyFn =\n typeof key === 'function' ? key : (d: T) => (d[key] as unknown) as number;\n\n return (items: T[]) => (items.length ? keyFn(items[0]) : undefined);\n}\n"],"names":[],"mappings":"eAIwC;AACtC,QAAM,QACJ,OAAO,QAAQ,aAAa,MAAM,CAAC,MAAU,EAAE;AAEjD,SAAO,CAAC,UAAgB,MAAM,SAAS,MAAM,MAAM,MAAM;AAAA;;;;"}
1
+ {"version":3,"file":"first.js","sources":["../../../src/summary/first.ts"],"sourcesContent":["/**\n * Returns a function that returns the first value for the specified key\n * @param key A string key of the object or an accessor converting the object to a number\n */\nexport function first<T extends object>(key: keyof T | ((d: T) => any)) {\n const keyFn = typeof key === 'function' ? key : (d: T) => d[key];\n\n return (items: T[]) => (items.length ? keyFn(items[0]) : undefined);\n}\n"],"names":[],"mappings":"eAIwC;AACtC,QAAM,QAAQ,OAAO,QAAQ,aAAa,MAAM,CAAC,MAAS,EAAE;AAE5D,SAAO,CAAC,UAAgB,MAAM,SAAS,MAAM,MAAM,MAAM;AAAA;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"last.js","sources":["../../../src/summary/last.ts"],"sourcesContent":["/**\n * Returns a function that returns the last value for the specified key\n * @param key A string key of the object or an accessor converting the object to a number\n */\nexport function last<T extends object>(key: keyof T | ((d: T) => number)) {\n const keyFn =\n typeof key === 'function' ? key : (d: T) => (d[key] as unknown) as number;\n\n return (items: T[]) =>\n items.length ? keyFn(items[items.length - 1]) : undefined;\n}\n"],"names":[],"mappings":"cAIuC;AACrC,QAAM,QACJ,OAAO,QAAQ,aAAa,MAAM,CAAC,MAAU,EAAE;AAEjD,SAAO,CAAC,UACN,MAAM,SAAS,MAAM,MAAM,MAAM,SAAS,MAAM;AAAA;;;;"}
1
+ {"version":3,"file":"last.js","sources":["../../../src/summary/last.ts"],"sourcesContent":["/**\n * Returns a function that returns the last value for the specified key\n * @param key A string key of the object or an accessor converting the object to a number\n */\nexport function last<T extends object>(key: keyof T | ((d: T) => any)) {\n const keyFn = typeof key === 'function' ? key : (d: T) => d[key];\n\n return (items: T[]) =>\n items.length ? keyFn(items[items.length - 1]) : undefined;\n}\n"],"names":[],"mappings":"cAIuC;AACrC,QAAM,QAAQ,OAAO,QAAQ,aAAa,MAAM,CAAC,MAAS,EAAE;AAE5D,SAAO,CAAC,UACN,MAAM,SAAS,MAAM,MAAM,MAAM,SAAS,MAAM;AAAA;;;;"}
@@ -1,9 +1,10 @@
1
1
  function roll(width, rollFn, options) {
2
- const {partial = false} = options != null ? options : {};
2
+ const {partial = false, align = "right"} = options != null ? options : {};
3
+ const halfWidth = Math.floor(width / 2);
3
4
  return (items) => {
4
5
  return items.map((_, i) => {
5
- const endIndex = i;
6
- if (!partial && endIndex - width + 1 < 0) {
6
+ const endIndex = align === "right" ? i : align === "center" ? i + halfWidth : i + width - 1;
7
+ if (!partial && (endIndex - width + 1 < 0 || endIndex >= items.length)) {
7
8
  return void 0;
8
9
  }
9
10
  const startIndex = Math.max(0, endIndex - width + 1);
@@ -1 +1 @@
1
- {"version":3,"file":"roll.js","sources":["../../../src/vector/roll.ts"],"sourcesContent":["type RollOptions = {\n partial?: boolean;\n};\n\n/**\n * Returns a function that computes the a rolling value (e.g. moving average) by\n * applying a function over a window of data\n * @param width The size of the window\n * @param rollFn The function to apply to the window (should reduce to a single value)\n * @param options Options to configure roll. e.g. whether to run on partial windows.\n */\nexport function roll<T extends object>(\n width: number,\n rollFn: (itemsInWindow: T[], endIndex: number) => any,\n options?: RollOptions | undefined | null\n) {\n const { partial = false } = options ?? {};\n\n return (items: any[]) => {\n return items.map((_, i) => {\n const endIndex = i;\n\n // partial window and we don't allow partial computation, return undefined\n if (!partial && endIndex - width + 1 < 0) {\n return undefined;\n }\n\n const startIndex = Math.max(0, endIndex - width + 1);\n const itemsInWindow = items.slice(startIndex, endIndex + 1);\n\n // reduce them to a single value\n return rollFn(itemsInWindow, endIndex);\n });\n };\n}\n"],"names":[],"mappings":"cAYE,OACA,QACA;AAEA,QAAM,CAAE,UAAU,SAAU,4BAAW;AAEvC,SAAO,CAAC;AACN,WAAO,MAAM,IAAI,CAAC,GAAG;AACnB,YAAM,WAAW;AAGjB,UAAI,CAAC,WAAW,WAAW,QAAQ,IAAI;AACrC,eAAO;AAAA;AAGT,YAAM,aAAa,KAAK,IAAI,GAAG,WAAW,QAAQ;AAClD,YAAM,gBAAgB,MAAM,MAAM,YAAY,WAAW;AAGzD,aAAO,OAAO,eAAe;AAAA;AAAA;AAAA;;;;"}
1
+ {"version":3,"file":"roll.js","sources":["../../../src/vector/roll.ts"],"sourcesContent":["type RollOptions = {\n partial?: boolean;\n /** which direction the window is aligned to (default: right, looking back)\n * - right: current row is the last item [1,2,**3**]\n * - left: current row is the first item [**1**,2,3]\n * - center: current row is the center item [1,**2**,3]\n */\n align?: 'left' | 'center' | 'right';\n};\n\n/**\n * Returns a function that computes the a rolling value (e.g. moving average) by\n * applying a function over a window of data\n * @param width The size of the window\n * @param rollFn The function to apply to the window (should reduce to a single value)\n * @param options Options to configure roll. e.g. whether to run on partial windows.\n */\nexport function roll<T extends object>(\n width: number,\n rollFn: (itemsInWindow: T[], endIndex: number) => any,\n options?: RollOptions | undefined | null\n) {\n const { partial = false, align = 'right' } = options ?? {};\n\n const halfWidth = Math.floor(width / 2);\n\n return (items: any[]) => {\n return items.map((_, i) => {\n const endIndex =\n align === 'right'\n ? i\n : align === 'center'\n ? i + halfWidth\n : i + width - 1;\n\n // partial window and we don't allow partial computation, return undefined\n if (!partial && (endIndex - width + 1 < 0 || endIndex >= items.length)) {\n return undefined;\n }\n\n const startIndex = Math.max(0, endIndex - width + 1);\n const itemsInWindow = items.slice(startIndex, endIndex + 1);\n\n // reduce them to a single value\n return rollFn(itemsInWindow, endIndex);\n });\n };\n}\n"],"names":[],"mappings":"cAkBE,OACA,QACA;AAEA,QAAM,CAAE,UAAU,OAAO,QAAQ,WAAY,4BAAW;AAExD,QAAM,YAAY,KAAK,MAAM,QAAQ;AAErC,SAAO,CAAC;AACN,WAAO,MAAM,IAAI,CAAC,GAAG;AACnB,YAAM,WACJ,UAAU,UACN,IACA,UAAU,WACV,IAAI,YACJ,IAAI,QAAQ;AAGlB,UAAI,CAAC,uBAAuB,QAAQ,IAAI,KAAK,YAAY,MAAM;AAC7D,eAAO;AAAA;AAGT,YAAM,aAAa,KAAK,IAAI,GAAG,WAAW,QAAQ;AAClD,YAAM,gBAAgB,MAAM,MAAM,YAAY,WAAW;AAGzD,aAAO,OAAO,eAAe;AAAA;AAAA;AAAA;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"first.js","sources":["../../../src/summary/first.ts"],"sourcesContent":["/**\n * Returns a function that returns the first value for the specified key\n * @param key A string key of the object or an accessor converting the object to a number\n */\nexport function first<T extends object>(key: keyof T | ((d: T) => number)) {\n const keyFn =\n typeof key === 'function' ? key : (d: T) => (d[key] as unknown) as number;\n\n return (items: T[]) => (items.length ? keyFn(items[0]) : undefined);\n}\n"],"names":[],"mappings":";;;;eAIwC;AACtC,QAAM,QACJ,OAAO,QAAQ,aAAa,MAAM,CAAC,MAAU,EAAE;AAEjD,SAAO,CAAC,UAAgB,MAAM,SAAS,MAAM,MAAM,MAAM;AAAA;;;;"}
1
+ {"version":3,"file":"first.js","sources":["../../../src/summary/first.ts"],"sourcesContent":["/**\n * Returns a function that returns the first value for the specified key\n * @param key A string key of the object or an accessor converting the object to a number\n */\nexport function first<T extends object>(key: keyof T | ((d: T) => any)) {\n const keyFn = typeof key === 'function' ? key : (d: T) => d[key];\n\n return (items: T[]) => (items.length ? keyFn(items[0]) : undefined);\n}\n"],"names":[],"mappings":";;;;eAIwC;AACtC,QAAM,QAAQ,OAAO,QAAQ,aAAa,MAAM,CAAC,MAAS,EAAE;AAE5D,SAAO,CAAC,UAAgB,MAAM,SAAS,MAAM,MAAM,MAAM;AAAA;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"last.js","sources":["../../../src/summary/last.ts"],"sourcesContent":["/**\n * Returns a function that returns the last value for the specified key\n * @param key A string key of the object or an accessor converting the object to a number\n */\nexport function last<T extends object>(key: keyof T | ((d: T) => number)) {\n const keyFn =\n typeof key === 'function' ? key : (d: T) => (d[key] as unknown) as number;\n\n return (items: T[]) =>\n items.length ? keyFn(items[items.length - 1]) : undefined;\n}\n"],"names":[],"mappings":";;;;cAIuC;AACrC,QAAM,QACJ,OAAO,QAAQ,aAAa,MAAM,CAAC,MAAU,EAAE;AAEjD,SAAO,CAAC,UACN,MAAM,SAAS,MAAM,MAAM,MAAM,SAAS,MAAM;AAAA;;;;"}
1
+ {"version":3,"file":"last.js","sources":["../../../src/summary/last.ts"],"sourcesContent":["/**\n * Returns a function that returns the last value for the specified key\n * @param key A string key of the object or an accessor converting the object to a number\n */\nexport function last<T extends object>(key: keyof T | ((d: T) => any)) {\n const keyFn = typeof key === 'function' ? key : (d: T) => d[key];\n\n return (items: T[]) =>\n items.length ? keyFn(items[items.length - 1]) : undefined;\n}\n"],"names":[],"mappings":";;;;cAIuC;AACrC,QAAM,QAAQ,OAAO,QAAQ,aAAa,MAAM,CAAC,MAAS,EAAE;AAE5D,SAAO,CAAC,UACN,MAAM,SAAS,MAAM,MAAM,MAAM,SAAS,MAAM;AAAA;;;;"}
@@ -3,11 +3,12 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  function roll(width, rollFn, options) {
6
- const {partial = false} = options != null ? options : {};
6
+ const {partial = false, align = "right"} = options != null ? options : {};
7
+ const halfWidth = Math.floor(width / 2);
7
8
  return (items) => {
8
9
  return items.map((_, i) => {
9
- const endIndex = i;
10
- if (!partial && endIndex - width + 1 < 0) {
10
+ const endIndex = align === "right" ? i : align === "center" ? i + halfWidth : i + width - 1;
11
+ if (!partial && (endIndex - width + 1 < 0 || endIndex >= items.length)) {
11
12
  return void 0;
12
13
  }
13
14
  const startIndex = Math.max(0, endIndex - width + 1);
@@ -1 +1 @@
1
- {"version":3,"file":"roll.js","sources":["../../../src/vector/roll.ts"],"sourcesContent":["type RollOptions = {\n partial?: boolean;\n};\n\n/**\n * Returns a function that computes the a rolling value (e.g. moving average) by\n * applying a function over a window of data\n * @param width The size of the window\n * @param rollFn The function to apply to the window (should reduce to a single value)\n * @param options Options to configure roll. e.g. whether to run on partial windows.\n */\nexport function roll<T extends object>(\n width: number,\n rollFn: (itemsInWindow: T[], endIndex: number) => any,\n options?: RollOptions | undefined | null\n) {\n const { partial = false } = options ?? {};\n\n return (items: any[]) => {\n return items.map((_, i) => {\n const endIndex = i;\n\n // partial window and we don't allow partial computation, return undefined\n if (!partial && endIndex - width + 1 < 0) {\n return undefined;\n }\n\n const startIndex = Math.max(0, endIndex - width + 1);\n const itemsInWindow = items.slice(startIndex, endIndex + 1);\n\n // reduce them to a single value\n return rollFn(itemsInWindow, endIndex);\n });\n };\n}\n"],"names":[],"mappings":";;;;cAYE,OACA,QACA;AAEA,QAAM,CAAE,UAAU,SAAU,4BAAW;AAEvC,SAAO,CAAC;AACN,WAAO,MAAM,IAAI,CAAC,GAAG;AACnB,YAAM,WAAW;AAGjB,UAAI,CAAC,WAAW,WAAW,QAAQ,IAAI;AACrC,eAAO;AAAA;AAGT,YAAM,aAAa,KAAK,IAAI,GAAG,WAAW,QAAQ;AAClD,YAAM,gBAAgB,MAAM,MAAM,YAAY,WAAW;AAGzD,aAAO,OAAO,eAAe;AAAA;AAAA;AAAA;;;;"}
1
+ {"version":3,"file":"roll.js","sources":["../../../src/vector/roll.ts"],"sourcesContent":["type RollOptions = {\n partial?: boolean;\n /** which direction the window is aligned to (default: right, looking back)\n * - right: current row is the last item [1,2,**3**]\n * - left: current row is the first item [**1**,2,3]\n * - center: current row is the center item [1,**2**,3]\n */\n align?: 'left' | 'center' | 'right';\n};\n\n/**\n * Returns a function that computes the a rolling value (e.g. moving average) by\n * applying a function over a window of data\n * @param width The size of the window\n * @param rollFn The function to apply to the window (should reduce to a single value)\n * @param options Options to configure roll. e.g. whether to run on partial windows.\n */\nexport function roll<T extends object>(\n width: number,\n rollFn: (itemsInWindow: T[], endIndex: number) => any,\n options?: RollOptions | undefined | null\n) {\n const { partial = false, align = 'right' } = options ?? {};\n\n const halfWidth = Math.floor(width / 2);\n\n return (items: any[]) => {\n return items.map((_, i) => {\n const endIndex =\n align === 'right'\n ? i\n : align === 'center'\n ? i + halfWidth\n : i + width - 1;\n\n // partial window and we don't allow partial computation, return undefined\n if (!partial && (endIndex - width + 1 < 0 || endIndex >= items.length)) {\n return undefined;\n }\n\n const startIndex = Math.max(0, endIndex - width + 1);\n const itemsInWindow = items.slice(startIndex, endIndex + 1);\n\n // reduce them to a single value\n return rollFn(itemsInWindow, endIndex);\n });\n };\n}\n"],"names":[],"mappings":";;;;cAkBE,OACA,QACA;AAEA,QAAM,CAAE,UAAU,OAAO,QAAQ,WAAY,4BAAW;AAExD,QAAM,YAAY,KAAK,MAAM,QAAQ;AAErC,SAAO,CAAC;AACN,WAAO,MAAM,IAAI,CAAC,GAAG;AACnB,YAAM,WACJ,UAAU,UACN,IACA,UAAU,WACV,IAAI,YACJ,IAAI,QAAQ;AAGlB,UAAI,CAAC,uBAAuB,QAAQ,IAAI,KAAK,YAAY,MAAM;AAC7D,eAAO;AAAA;AAGT,YAAM,aAAa,KAAK,IAAI,GAAG,WAAW,QAAQ;AAClD,YAAM,gBAAgB,MAAM,MAAM,YAAY,WAAW;AAGzD,aAAO,OAAO,eAAe;AAAA;AAAA;AAAA;;;;"}
package/dist/tidy.d.ts CHANGED
@@ -1282,6 +1282,12 @@ declare function cumsum<T extends object>(key: keyof T | ((d: T) => number | nul
1282
1282
 
1283
1283
  declare type RollOptions = {
1284
1284
  partial?: boolean;
1285
+ /** which direction the window is aligned to (default: right, looking back)
1286
+ * - right: current row is the last item [1,2,**3**]
1287
+ * - left: current row is the first item [**1**,2,3]
1288
+ * - center: current row is the center item [1,**2**,3]
1289
+ */
1290
+ align?: 'left' | 'center' | 'right';
1285
1291
  };
1286
1292
  /**
1287
1293
  * Returns a function that computes the a rolling value (e.g. moving average) by
@@ -1387,13 +1393,13 @@ declare function nDistinct<T extends object>(key: keyof T | ((d: T) => any), opt
1387
1393
  * Returns a function that returns the first value for the specified key
1388
1394
  * @param key A string key of the object or an accessor converting the object to a number
1389
1395
  */
1390
- declare function first<T extends object>(key: keyof T | ((d: T) => number)): (items: T[]) => number | undefined;
1396
+ declare function first<T extends object>(key: keyof T | ((d: T) => any)): (items: T[]) => any;
1391
1397
 
1392
1398
  /**
1393
1399
  * Returns a function that returns the last value for the specified key
1394
1400
  * @param key A string key of the object or an accessor converting the object to a number
1395
1401
  */
1396
- declare function last<T extends object>(key: keyof T | ((d: T) => number)): (items: T[]) => number | undefined;
1402
+ declare function last<T extends object>(key: keyof T | ((d: T) => any)): (items: T[]) => any;
1397
1403
 
1398
1404
  /**
1399
1405
  * Returns all keys
package/dist/umd/tidy.js CHANGED
@@ -1139,11 +1139,12 @@
1139
1139
  }
1140
1140
 
1141
1141
  function roll(width, rollFn, options) {
1142
- const {partial = false} = options != null ? options : {};
1142
+ const {partial = false, align = "right"} = options != null ? options : {};
1143
+ const halfWidth = Math.floor(width / 2);
1143
1144
  return (items) => {
1144
1145
  return items.map((_, i) => {
1145
- const endIndex = i;
1146
- if (!partial && endIndex - width + 1 < 0) {
1146
+ const endIndex = align === "right" ? i : align === "center" ? i + halfWidth : i + width - 1;
1147
+ if (!partial && (endIndex - width + 1 < 0 || endIndex >= items.length)) {
1147
1148
  return void 0;
1148
1149
  }
1149
1150
  const startIndex = Math.max(0, endIndex - width + 1);