purify-objects
Version:
A powerful TypeScript library for cleaning objects by removing empty values, with support for YAML and CSV formats
187 lines (147 loc) • 4.58 kB
Markdown
[](https://www.npmjs.com/package/purify-objects)
[](https://www.npmjs.com/package/purify-objects)
[](https://bundlephobia.com/package/purify-objects)
[](https://opensource.org/licenses/MIT)
[](https://www.typescriptlang.org/)
[](https://github.com/ml7s/purify-objects/pulls)
A powerful TypeScript library for cleaning objects by removing empty values, with support for YAML and CSV formats.
- Clean objects by removing empty values (null, undefined, empty strings, empty arrays, empty objects)
- Support for JSON, YAML, and CSV formats
- Format conversion between JSON, YAML, and CSV
- Command-line interface (CLI) for file processing
- Safe mode to prevent accidental modifications
- Compare mode to preview changes
- Custom cleaning options
- Automatic format detection based on file extension
## Installation
```bash
npm install purify-objects
```
## Usage
### As a Library
```typescript
import { cleanObject, parseContent, stringifyContent } from 'purify-objects';
const obj = {
id: 1,
name: "Turki",
meta: {
age: null,
location: {
city: "Riyadh",
coords: undefined,
district: "",
},
preferences: {},
},
roles: [],
lastLogin: "2024-01-15",
status: 0
};
const cleaned = cleanObject(obj);
const yaml = `
version: 2
config:
user: Turki
env: prod
paths:
root: /app
temp: null
logs:
plugins: []
`;
const parsed = parseContent(yaml, 'yaml');
const result = cleanObject(parsed);
const output = stringifyContent(result, 'yaml');
const csv = `
id,name,department,salary,active
1,Turki,IT,15000,true
2,Ahmad,HR,,false
3,Mohammed,IT,12000,
4,,Finance,,true
`;
const data = parseContent(csv, 'csv', { headers: true });
const processed = data.map(row => cleanObject(row));
const final = stringifyContent(processed, 'csv', { headers: true });
```
Basic usage:
```bash
npx purify-objects input.json
npx purify-objects input.yaml
npx purify-objects input.csv
```
Save output to a file:
```bash
npx purify-objects input.json --output cleaned.json
```
Convert between formats:
```bash
npx purify-objects input.json --convert-to yaml
npx purify-objects input.yaml --convert-to csv
npx purify-objects input.csv --convert-to json
```
Convert without cleaning:
```bash
npx purify-objects input.yaml --convert-to json --noclean
```
```bash
npx purify-objects input.csv --delimiter ";"
npx purify-objects input.csv --no-headers
```
```bash
npx purify-objects input.json --compare
npx purify-objects input.json --safe
npx purify-objects input.json --remove-zero-values
```
- `--output <file>`: Save output to a file
- `--format <format>`: Specify input format (json, yaml, csv)
- `--convert-to <format>`: Convert to specified format
- `--noclean`: Convert without cleaning data
- `--compare`: Preview changes without modifying
- `--safe`: Create new file instead of modifying
- `--delimiter <char>`: CSV delimiter (default: ",")
- `--no-headers`: Process CSV without headers
- `--remove-zero-values`: Remove fields with zero values
## API
```typescript
function cleanObject<T extends object>(
obj: T,
customCleaner?: (key: string, value: any) => boolean,
keepFields?: string[],
options?: CleanerOptions
): T;
function parseContent(
content: string,
format: 'json' | 'yaml' | 'csv',
options?: ParserOptions
): object | object[];
function stringifyContent(
data: object | object[],
format: 'json' | 'yaml' | 'csv',
options?: ParserOptions
): string;
interface CleanerOptions {
recursive?: boolean;
safe?: boolean;
}
interface ParserOptions {
delimiter?: string;
headers?: boolean;
}
```
MIT