1- type PlainObject = Record < string , any >
1+ /**
2+ * Represents any JSON-serializable value (primitives, arrays, or objects).
3+ * Used internally for type-safe JSON splitting operations.
4+ */
5+ type JsonValue =
6+ | string
7+ | number
8+ | boolean
9+ | null
10+ | undefined
11+ | JsonValue [ ]
12+ | { [ key : string ] : JsonValue }
13+
14+ type PlainObject = Record < string , JsonValue >
215
316interface Chunk < T > {
417 data : T
518 length : number
619}
720
8- function isPlainObject ( val : any ) : val is PlainObject {
21+ function isPlainObject ( val : unknown ) : val is PlainObject {
922 return (
1023 typeof val === 'object' &&
1124 val !== null &&
1225 Object . getPrototypeOf ( val ) === Object . prototype
1326 )
1427}
1528
16- function getJsonSize ( data : any ) : number {
29+ function getJsonSize ( data : unknown ) : number {
1730 if ( data === undefined ) {
1831 return 'undefined' . length
1932 }
@@ -93,7 +106,7 @@ function splitObject(params: {
93106 } )
94107
95108 for ( const [ index , item ] of items . entries ( ) ) {
96- const itemWithKey : Chunk < any > = {
109+ const itemWithKey : Chunk < PlainObject > = {
97110 data : { [ key ] : item . data } ,
98111 length : item . length + overhead ,
99112 }
@@ -155,14 +168,14 @@ function splitObject(params: {
155168 return chunks
156169}
157170
158- function splitArray ( params : { arr : any [ ] ; maxSize : number } ) : Chunk < any [ ] > [ ] {
171+ function splitArray ( params : { arr : JsonValue [ ] ; maxSize : number } ) : Chunk < JsonValue [ ] > [ ] {
159172 const { arr, maxSize } = params
160- const chunks : Chunk < any [ ] > [ ] = [ ]
161- let currentChunk : Chunk < any [ ] > = { data : [ ] , length : 2 }
173+ const chunks : Chunk < JsonValue [ ] > [ ] = [ ]
174+ let currentChunk : Chunk < JsonValue [ ] > = { data : [ ] , length : 2 }
162175
163176 for ( const element of arr ) {
164- const entryArr = [ element ]
165- const standaloneEntry : Chunk < any [ ] > = {
177+ const entryArr : JsonValue [ ] = [ element ]
178+ const standaloneEntry : Chunk < JsonValue [ ] > = {
166179 data : entryArr ,
167180 length : getJsonSize ( entryArr ) ,
168181 }
@@ -224,27 +237,29 @@ function splitArray(params: { arr: any[]; maxSize: number }): Chunk<any[]>[] {
224237}
225238
226239function splitDataWithLengths ( params : {
227- data : any
240+ data : unknown
228241 maxChunkSize : number
229- } ) : Chunk < any > [ ] {
242+ } ) : Chunk < JsonValue > [ ] {
230243 const { data, maxChunkSize } = params
231244 // Handle primitives
232245 if ( typeof data !== 'object' || data === null ) {
233246 if ( typeof data === 'string' ) {
234247 const result = splitString ( { data, maxSize : maxChunkSize } )
235248 return result
236249 }
237- return [ { data, length : getJsonSize ( data ) } ]
250+ // Primitives (number, boolean, null, undefined) are valid JsonValues
251+ return [ { data : data as JsonValue , length : getJsonSize ( data ) } ]
238252 }
239253
240- // Non-plain objects (Date, RegExp, etc.)
254+ // Non-plain objects (Date, RegExp, etc.) - pass through as-is
255+ // These will be serialized by JSON.stringify when needed
241256 if ( ! Array . isArray ( data ) && ! isPlainObject ( data ) ) {
242- return [ { data, length : getJsonSize ( data ) } ]
257+ return [ { data : data as JsonValue , length : getJsonSize ( data ) } ]
243258 }
244259
245260 // Arrays
246261 if ( Array . isArray ( data ) ) {
247- const result = splitArray ( { arr : data , maxSize : maxChunkSize } )
262+ const result = splitArray ( { arr : data as JsonValue [ ] , maxSize : maxChunkSize } )
248263 return result
249264 }
250265
@@ -253,7 +268,15 @@ function splitDataWithLengths(params: {
253268 return result
254269}
255270
256- export function splitData ( params : { data : any ; maxChunkSize ?: number } ) : any [ ] {
271+ /**
272+ * Splits JSON-serializable data into smaller chunks that fit within the specified size limit.
273+ * Preserves the structure of objects and arrays while splitting long strings and nested values.
274+ *
275+ * @param params.data - The data to split (can be any JSON-serializable value)
276+ * @param params.maxChunkSize - Maximum size in characters for each chunk (default: 99,000)
277+ * @returns An array of chunks, each fitting within the size limit
278+ */
279+ export function splitData ( params : { data : unknown ; maxChunkSize ?: number } ) : unknown [ ] {
257280 const { data, maxChunkSize = 99_000 } = params
258281 return splitDataWithLengths ( { data, maxChunkSize } ) . map ( ( cwjl ) => cwjl . data )
259282}
0 commit comments