Socket
Socket
Sign inDemoInstall

@shelf/array-chunk-by-size

Package Overview
Dependencies
1
Maintainers
61
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.0.0 to 3.1.0

6

lib/index.d.ts

@@ -0,1 +1,2 @@

type SizeCalcFunction<T> = (obj: T) => number;
/**

@@ -6,8 +7,11 @@ * Chunk array of objects by their size when stringifies into JSON

* @param {Boolean} failOnOversize Throw error if item is too big
* @param {Function} sizeCalcFunction Custom function to calculate size of each item
* @return {Object[][]} Array of arrays - chunked array by size
*/
export declare function chunkArray<T>({ input, bytesSize, failOnOversize, }: {
export declare function chunkArray<T>({ input, bytesSize, failOnOversize, sizeCalcFunction, }: {
input: T[];
bytesSize?: number;
failOnOversize?: boolean;
sizeCalcFunction?: SizeCalcFunction<T>;
}): T[][];
export {};

12

lib/index.js

@@ -7,4 +7,4 @@ "use strict";

exports.chunkArray = chunkArray;
const stringify = require('json-stringify-safe');
var _jsonStringifySafe = _interopRequireDefault(require("json-stringify-safe"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**

@@ -15,2 +15,3 @@ * Chunk array of objects by their size when stringifies into JSON

* @param {Boolean} failOnOversize Throw error if item is too big
* @param {Function} sizeCalcFunction Custom function to calculate size of each item
* @return {Object[][]} Array of arrays - chunked array by size

@@ -21,3 +22,4 @@ */

bytesSize = Number.MAX_SAFE_INTEGER,
failOnOversize = false
failOnOversize = false,
sizeCalcFunction = getObjectSize
}) {

@@ -31,3 +33,3 @@ const output = [];

for (const obj of input) {
const objSize = getObjectSize(obj);
const objSize = sizeCalcFunction(obj);
if (objSize > bytesSize && failOnOversize) {

@@ -57,3 +59,3 @@ throw new Error(`Can't chunk array as item is bigger than the max chunk size`);

try {
const str = stringify(obj);
const str = (0, _jsonStringifySafe.default)(obj);
return Buffer.byteLength(str, 'utf8');

@@ -60,0 +62,0 @@ } catch (error) {

{
"name": "@shelf/array-chunk-by-size",
"version": "3.0.0",
"version": "3.1.0",
"description": "Chunk array of objects by their size in JSON",

@@ -45,4 +45,4 @@ "keywords": [

"devDependencies": {
"@babel/cli": "7.19.3",
"@babel/core": "7.20.2",
"@babel/cli": "7.23.0",
"@babel/core": "7.23.0",
"@shelf/babel-config": "1.2.0",

@@ -54,9 +54,10 @@ "@shelf/eslint-config": "2.22.0",

"@types/jest": "28.1.8",
"@types/json-stringify-safe": "5.0.1",
"@types/node": "16",
"eslint": "8.26.0",
"husky": "8.0.2",
"eslint": "8.50.0",
"husky": "8.0.3",
"jest": "28.1.3",
"lint-staged": "13.0.3",
"prettier": "2.7.1",
"typescript": "4.8.4"
"lint-staged": "13.3.0",
"prettier": "2.8.8",
"typescript": "4.9.5"
},

@@ -63,0 +64,0 @@ "engines": {

@@ -8,3 +8,3 @@ # array-chunk-by-size [![CircleCI](https://img.shields.io/circleci/project/shelfio/array-chunk-by-size.svg)](https://circleci.com/gh/shelfio/array-chunk-by-size)

```
$ yarn add array-chunk-by-size
$ yarn add @shelf/array-chunk-by-size
```

@@ -19,3 +19,3 @@

```js
import {chunkArray} from 'array-chunk-by-size';
import {chunkArray} from '@shelf/array-chunk-by-size';

@@ -29,3 +29,22 @@ const bigArray = [{a: 1}, {b: 2}, {c: 3}];

Alternatively, you might pass a custom size calculation function.
For example, to chunk array by LLM tokens size:
```js
import {chunkArray} from '@shelf/array-chunk-by-size';
import {encode} from 'gpt-3-encoder';
const bigArray = ['msg-1', 'msg-2'];
const gpt3MaxTokens = 4000;
const smallerArrays = chunkArray({
input: bigArray,
bytesSize: gpt3MaxTokens,
sizeCalcFunction: item => encode(item).length,
});
```
## See Also
- [fast-chunk-string](https://github.com/shelfio/fast-chunk-string)
- [fast-normalize-spaces](https://github.com/shelfio/fast-normalize-spaces)

@@ -32,0 +51,0 @@ - [fast-natural-order-by](https://github.com/shelfio/fast-natural-order-by)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc