New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@uniiem/object-trim

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@uniiem/object-trim

Calculate and trim object size by specifying properties

latest
npmnpm
Version
0.2.0
Version published
Maintainers
1
Created
Source

@uniiem/object-trim

GitHub Actions Workflow Status NPM Downloads NPM Version (with dist tag) npm bundle size GitHub License

Calculate and trim object size by specifying properties

It can be used for the context length limit of LLM Chatbot histories.

Usage

npm i @uniiem/object-trim
// import trimObject
import { trimObject } from '@uniiem/object-trim'

// A object list, e.g. LLM chatbot history
const case_obj_list = [
  {
    id: 'fb3dd9ed-52bf-4aff-b4be-2ac9ddc95319',
    role: 'user',
    content: 'How is the weather today?'
  },
  {
    id: '33643516-554b-453c-8375-4e032fe07232',
    role: 'assistant',
    content: 'It is sunny today.'
  },
  {
    id: '78af377f-aa79-453a-b0df-0ef702c8f3a6',
    role: 'user',
    content: 'What is the temperature?'
  },
  {
    id: '3d722395-3ab8-423f-a290-9a132a3c6dd7',
    role: 'assistant',
    content: 'It is 25 degrees.'
  }
]

// Trim the object list by the key 'content' and max length 50
const trimmed_obj_list = trimObject(case_obj_list, 50, { keys: ['content'] })
console.log(trimmed_obj_list)

// output
/*
  [
    {
      id: '78af377f-aa79-453a-b0df-0ef702c8f3a6',
      role: 'user',
      content: 'What is the temperature?'
    },
    {
      id: '3d722395-3ab8-423f-a290-9a132a3c6dd7',
      role: 'assistant',
      content: 'It is 25 degrees.'
    }
  ]
 */

API

trimObject(objectList, maxLength, TrimObjectProperties[])

const trimObject: <T>(objectList: T[], maxLength: number, props: TrimObjectProperties<T>) => T[];

interface TrimObjectProperties<T> {
    keys: (keyof T)[];
    fromStart?: boolean;  // default: false
}

Keywords

object

FAQs

Package last updated on 02 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts