# data-manipulator

> Library that manipulates veriety of data structures. Right now replace and sort are available.

Latest version **1.2.4** (published 2019-04-25) · ISC license · 0 weekly downloads

## Install

```sh
npm install data-manipulator
pnpm add data-manipulator
yarn add data-manipulator
bun add data-manipulator
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.4 |
| Published | 2019-04-25 |
| First published | 2019-03-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 96.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Michael Dmitriev |
| Maintainers | dmik |
| Keywords | replace, sort, array, object, data, manipulator, utils, tools |

## Links

- npm: https://www.npmjs.com/package/data-manipulator
- npm.io page: https://npm.io/package/data-manipulator

## Dependencies (5)

- [chai](https://npm.io/package/chai.md) ^4.2.0
- [mocha](https://npm.io/package/mocha.md) ^6.1.4
- [moment](https://npm.io/package/moment.md) ^2.24.0
- [mongodb](https://npm.io/package/mongodb.md) ^3.2.3
- [mongoskin](https://npm.io/package/mongoskin.md) ^2.1.0

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 1.2.4 (latest) — 2019-04-25
- 1.2.3 — 2019-04-25
- 1.2.2 — 2019-04-25
- 1.2.1 — 2019-04-25
- 1.2.0 — 2019-04-25
- 1.1.6 — 2019-03-30
- 1.1.5 — 2019-03-30
- 1.1.4 — 2019-03-30
- 1.1.3 — 2019-03-30
- 1.1.2 — 2019-03-30
- 1.1.1 — 2019-03-30
- 1.1.0 — 2019-03-30
- 1.0.15 — 2019-03-29
- 1.0.14 — 2019-03-29
- 1.0.13 — 2019-03-29
- … 13 more at https://npm.io/package/data-manipulator/versions

## README

# Data Manipulator

Data Manipulator is a library that helps you to manipulate all none Primitive data types.

## Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

### Installing

npm install data-manipulator

### Requiring library
```
const { DM } = require("data-manipulator");
```
### Predefine configurations

Important! With this option enabled function Set(data) will copy data to a newData via JSON.parse(JSON.stringify(object)).
Therefore, the newData returned from Get() will be stingified!
```
const dataManipulator = new DM();
```

or

```
const dataManipulator = new DM({
    enableDeepCopy: true
});
```

Will not override input data

```
const dataManipulator2 = new DM({
    enableDeepCopy: false
});
```

### Replace:

#### Function that search strings by given regular expression and manipulates them to something new.

An example for an actual usage in the tests layer: By warping all arrays that contains mongo documents to a one huge object.
As a result all the fake data gets manipulated into data that is valid for mongo insert.

```
dataManipulator2
	.Set({ fakeFactorySpecialRequestsArray, fakeUsersArray, fakeTasksArray })
	.Replace(DM.MongoIdRGX, (id) => mongoHelper.toObjectID(id))
	.Replace(DM.ISODateRGX, (date) => moment.tz(date, 'Asia/Jerusalem').toDate())
```

### DeepSort:
#### Function that sorts data via schema. (follow the examples in order to understand better)

```
const outputData = dataManipulator
	.Set(inputData)
	.DeepSort(sortSchema)
	.Get();
```

#### sortByField: What field to sort.

```
const outputData = dataManipulator
	.Set([5.5, "5.4", "av", 9.99])
	.DeepSort({
		sortByField: DM.PrimitiveValue
	})
	.Get();
outputData: ["5.4", 5.5, 9.99, "av"]
```

```
const outputData = dataManipulator
	.Set([{ number: 7 }, { number: 2 }, { number: 5 }])
	.DeepSort({
		sortByField: "number"
	})
	.Get();
outputData: [{ number: 2 }, { number: 5 }, { number: 7 }]
```

#### fieldName & sortDataArray: Where to sort.

```
const outputData = dataManipulator
	.Set({
		values: [5, 2, 7]
	})
	.DeepSort({
		fieldName: "values",
		sortByField: DM.PrimitiveValue
	})
	.Get();
outputData: {
	values: [2, 5, 7]
}
```

```
const outputData = dataManipulator
	.Set([
		{ value: "B", numbers: [26, 7, 23], anotherValue: 4 }, 
		{ numbers: [2,5,1], value: 8 }, 
		{ numbers: [2,5,1], value: 5 },
		{ value: "B", numbers: [2,5,1], anotherValue: "C" }, 
	])
	.DeepSort({
		sortByField: ["value", "anotherValue"],
		sortDataArray: {
			fieldName: "numbers",
			sortByField: DM.PrimitiveValue
		}
	})
	.Get();
outputData: [
	{ numbers: [1,2,5], value: 5 },
	{ numbers: [1,2,5], value: 8 }, 
	{ value: "B", numbers: [7, 23, 26], anotherValue: 4 }, 
	{ value: "B", numbers: [1,2,5], anotherValue: "C" }, 
]
```

```
const outputData = dataManipulator
	.Set([
		{ numbers: [2,5,1] },
		{ numbers: [2,5,1] },
		{ numbers: [2,5,1] }
	])
	.DeepSort({
		sortDataArray: [{
			fieldName: "numbers",
			sortByField: DM.PrimitiveValue
		}]
	})
	.Get();
outputData: [
	{ numbers: [1,2,5] },
	{ numbers: [1,2,5] },
	{ numbers: [1,2,5] }
]
```

```
const outputData = dataManipulator
	.Set([
		{ value: "B", numbers: [2,5,1] },
		{ numbers: [2,5,1], value: 8 },
		{ numbers: [2,5,1], value: 5 }
	])
	.DeepSort({
		sortByField: "value",
		sortDataArray: {
			fieldName: "numbers",
			sortByField: DM.PrimitiveValue
		}
	})
	.Get();
outputData: [
	{"value":5,"numbers":[1,2,5]},
	{"numbers":[1,2,5],"value":8},
	{"numbers":[1,2,5],"value":"B"}
]
```

```
const outputData = dataManipulator
	.Set([
		{ value: "B", numbers: [2,5,1] }, 
		{ numbers: [2,5,1], value: 8 }, 
		{ numbers: [2,5,1], value: 5 }
	])
	.DeepSort([{
		fieldName: 0,
		sortDataArray: {
			fieldName: "numbers",
			sortByField: DM.PrimitiveValue
		}
	},{
		sortByField: "value",
	}])
	.Get();
outputData: [
	{"value":5,"numbers":[2,5,1]},
	{"numbers":[2,5,1],"value":8},
	{"numbers":[1,2,5],"value":"B"}
]
```

```
const outputData = dataManipulator
	.Set({
		name: {
			values: [5, 2, 7]
		}
	})
	.DeepSort({
		fieldName: "name",
		sortDataArray: {
			fieldName: "values",
			sortByField: DM.PrimitiveValue
		}
	})
	.Get();
outputData: {
	name: {
		values: [2, 5, 7]
	}
}
```

```
const outputData = dataManipulator
	.Set({
		name: {
			values: [{ number: 7 }, { number: 2 }, { number: 5 }]
		}
	})
	.DeepSort({
		fieldName: "name",
		sortDataArray: {
			fieldName: "values",
			sortByField: "number"
		}
	})
	.Get();
outputData: {
	name: {
		values: [{ number: 2 }, { number: 5 }, { number: 7 }]
	}
}
```

```
const outputData = dataManipulator
	.Set([
		{ value: "B", numbers: [26, 7, 23], anotherValue: 4, thirdValue: "5" },
		{ numbers: [2,5,1], value: 8 },
		{ numbers: [2,5,1], value: 5 },
		{ value: "B", numbers: [26, 7, 23], anotherValue: 4, thirdValue: "8" },
		{ value: "B", numbers: [26, 7, 23], anotherValue: 4, thirdValue: "FEWF" },
		{ value: "B", numbers: [26, 7, 23], anotherValue: 4, thirdValue: "5.14" },
		{ value: "B", numbers: [2,5,1], anotherValue: "C" },
		{ value: "B", numbers: [26, 7, 23], anotherValue: 4, thirdValue: "8" },
	])
	.DeepSort({
		sortByField: ["value", "anotherValue", "thirdValue"],
		sortDataArray: {
			fieldName: "numbers",
			sortByField: DM.PrimitiveValue
		}
	})
	.Get();
outputData: [
	{ numbers: [1,2,5], value: 5 },
	{ numbers: [1,2,5], value: 8 }, 
	{ value: "B", numbers: [7, 23, 26], anotherValue: 4, thirdValue: "5" }, 
	{ value: "B", numbers: [7, 23, 26], anotherValue: 4, thirdValue: "5.14" }, 
	{ value: "B", numbers: [7, 23, 26], anotherValue: 4, thirdValue: "8" }, 
	{ value: "B", numbers: [7, 23, 26], anotherValue: 4, thirdValue: "8" },
	{ value: "B", numbers: [7, 23, 26], anotherValue: 4, thirdValue: "FEWF" }, 
	{ value: "B", numbers: [1,2,5], anotherValue: "C" }, 
]
```

```
const outputData = dataManipulator
	.Set([{ value: "B", numbers: [2,5,1] }, { numbers: [2,5,1], value: 8 }, { numbers: [2,5,1], value: 5 }])
	.DeepSort([{
		sortDataArray: {
			fieldName: "numbers",
			sortByField: DM.PrimitiveValue
		}
	},{
		sortByField: "value",
	}])
	.Get();
outputData: [{"value":5,"numbers":[1,2,5]},{"numbers":[1,2,5],"value":8},{"numbers":[1,2,5],"value":"B"}]
```

```
const outputData = dataManipulator
	.Set([{ value: "B", numbers: [2,5,1] }, { numbers: [2,5,1], value: 8 }, { numbers: [2,5,1], value: 5 }])
	.DeepSort([{
			sortByField: "value",
		},
		{
			sortDataArray: {
				fieldName: "numbers",
				sortByField: DM.PrimitiveValue
			}
		}
	])
	.Get();
outputData: [{"value":5,"numbers":[1,2,5]},{"numbers":[1,2,5],"value":8},{"numbers":[1,2,5],"value":"B"}]
```

```
const outputData = dataManipulator
	.Set({
		mike: {
			value: [{ val: 5 }, { val: 3 }, { val: 9 }],
			data: [6, "4", "ER", 5.43, "5.4"]
		},
		pike: {
			hike: {
				bike: {
					unsortedArray: [6, 4, 3, 7, 1]
				}
			},
			kick: {}
		}
	})
	.DeepSort([{
		fieldName: "mike",
		sortDataArray: [{
			fieldName: "value",
			sortByFunction: (a, b) => {
				return b.val - a.val;
			}
		}, {
			fieldName: "data",
			sortByField: DM.PrimitiveValue
		}]
	},
	{
		fieldName: "pike",
		sortDataArray: [{
			fieldName: "hike",
			sortDataArray: [{
				fieldName: "bike",
				sortDataArray: [{
					fieldName: "unsortedArray",
					sortByField: DM.PrimitiveValue
				}]
			}]
		}]
	}])
	.Get();
outputData: {
	mike: {
		value: [{ val: 9 }, { val: 5 }, { val: 3 }],
		data: ["4", "5.4", 5.43, 6, "ER"]
	},
	pike: {
		hike: {
			bike: {
				unsortedArray: [1,3,4,6,7]
			}
		},
		kick: {}
	}
}
```

```
const outputData = dataManipulator
	.Set([
		{ value: "B", numbers: [2,5,1] }, 
		{ numbers: [2,5,1], value: 8 }, 
		{ numbers: [2,5,1], value: 5 }
	])
	.DeepSort([{
		fieldName: 1,
		sortDataArray: {
			fieldName: "numbers",
			sortByField: DM.PrimitiveValue
		}
	},{
		sortByField: "value",
	}])
	.Get();
outputData: [
	{"value":5,"numbers":[2,5,1]},
	{"numbers":[1,2,5],"value":8},
	{"numbers":[2,5,1],"value":"B"}
]
```

#### sortByFunction

```
const outputData = dataManipulator
	.Set([5, 2, 7])
	.DeepSort({
		sortByFunction: (a, b) => b - a,
	})
	.Get();
outputData: [7, 5, 2]
```

```
const outputData = dataManipulator
	.Set({
		value: [{ val: 5 }, { val: 3 }, { val: 9 }]
	})
	.DeepSort({
		fieldName: "value",
		sortByFunction: (a, b) => {
			return b.val - a.val;
		}
	})
	.Get();
outputData: {
	value: [{ val: 9 }, { val: 5 }, { val: 3 }]
}
```

#### sortByReverse

```
const outputData = dataManipulator
	.Set([
		{ value: "B", numbers: [26, 7, 23], anotherValue: 4, thirdValue: "5" },
		{ numbers: [2,5,1], value: 8 },
		{ numbers: [2,5,1], value: 5 },
		{ value: "B", numbers: [26, 7, 23], anotherValue: 4, thirdValue: "8" },
		{ value: "B", numbers: [26, 7, 23], anotherValue: 4, thirdValue: "FEWF" },
		{ value: "B", numbers: [26, 7, 23], anotherValue: 4, thirdValue: "5.14" },
		{ value: "B", numbers: [2,5,1], anotherValue: "C" },
		{ value: "B", numbers: [26, 7, 23], anotherValue: 4, thirdValue: "8" },
	])
	.DeepSort({
		sortByField: ["value", "anotherValue", "thirdValue"],
		sortByReverse: true,
		sortDataArray: {
			fieldName: "numbers",
			sortByField: DM.PrimitiveValue
		}
	})
	.Get();
outputData: [
	{ value: "B", numbers: [1,2,5], anotherValue: "C" }, 
	{ value: "B", numbers: [7, 23, 26], anotherValue: 4, thirdValue: "FEWF" }, 
	{ value: "B", numbers: [7, 23, 26], anotherValue: 4, thirdValue: "8" }, 
	{ value: "B", numbers: [7, 23, 26], anotherValue: 4, thirdValue: "8" },
	{ value: "B", numbers: [7, 23, 26], anotherValue: 4, thirdValue: "5.14" }, 
	{ value: "B", numbers: [7, 23, 26], anotherValue: 4, thirdValue: "5" }, 
	{ numbers: [1,2,5], value: 8 }, 
	{ numbers: [1,2,5], value: 5 },
]
```

#### fieldPath: Alternative to sortDataArray & fieldName.

```
const outputData = dataManipulator
	.Set([{
		values: [{
			moreValues: {
				a: [5, 2, 7],
				b: [5, 2, 7],
				c: [5, 2, 7],
				d: [5, 2, 7],
				e: [5, 2, 7]
			}
		}],
		anotherKEy: "fwefwe"
	}])
	.DeepSort({
		fieldPath: "-.values.-.-.-",
		sortByField: DM.PrimitiveValue,
	})
	.Get();
outputData: [
	{
		values: [{
			moreValues: {
				a: [2, 5, 7],
				b: [2, 5, 7],
				c: [2, 5, 7],
				d: [2, 5, 7],
				e: [2, 5, 7]
			}
		}],
		anotherKEy: "fwefwe"
}]
```

```
const outputData = dataManipulator
	.Set([
            {
                "Apartments": [
                    {
                        "Doors": [{doorId:5},{doorId:2},{doorId:7},{doorId:3},{doorId:10},{doorId:1}],
                    }
                ],
            },
            {
                "Apartments": [
                    {
                        "Doors": [{doorId:5},{doorId:2},{doorId:7},{doorId:3},{doorId:10},{doorId:1}],
                    },
                    {
                        "Doors": [{doorId:5},{doorId:2},{doorId:7},{doorId:3},{doorId:10},{doorId:1}],
                    },
                    {
                        "Doors": [{doorId:5},{doorId:2},{doorId:7},{doorId:3},{doorId:10},{doorId:1}],
                    }
                ]
            }
	])
	.DeepSort({
            fieldPath: "[1].Apartments[1].Doors",
            sortByField: "doorId",
        })
	.Get();
outputData:[
	{
		"Apartments": [
			{
				"Doors": [{doorId:5},{doorId:2},{doorId:7},{doorId:3},{doorId:10},{doorId:1}],
			}
		],
	},
	{
		"Apartments": [
			{
				"Doors": [{doorId:5},{doorId:2},{doorId:7},{doorId:3},{doorId:10},{doorId:1}],
			},
			{
				"Doors": [{doorId:1},{doorId:2},{doorId:3},{doorId:5},{doorId:7},{doorId:10}],
			},
			{
				"Doors": [{doorId:5},{doorId:2},{doorId:7},{doorId:3},{doorId:10},{doorId:1}],
			}
		]
	}
]
```

### 1.1.0 version updates:

Removed unnessesary required sortByField field when using sortByFunction.

```
sortByField: "val", // unnessesary
sortByFunction: (a, b) => { //when you make your own sort function
	return b.val - a.val;
}
```

DeepSort(sortDataArray) and sortDataArray inside schema: can now get a single data object instead of array with objects.
It's a shortcut for single sortData item.

```
const outputData = dataManipulator
	.Set([
		{ value: "B", numbers: [26, 7, 23], anotherValue: 4 }, 
		{ numbers: [2,5,1], value: 8 }, 
		{ numbers: [2,5,1], value: 5 },
		{ value: "B", numbers: [2,5,1], anotherValue: "C" }, 
	])
	.DeepSort({ //update 1.1.0 -> no need to use array of objects
		sortByField: ["value", "anotherValue"],
		sortDataArray: { //update 1.1.0 -> no need to use array of objects
			fieldName: "numbers",
			sortByField: DM.PrimitiveValue
		}
	})
	.Get();
outputData: [
	{ numbers: [1,2,5], value: 5 },
	{ numbers: [1,2,5], value: 8 }, 
	{ value: "B", numbers: [7, 23, 26], anotherValue: 4 }, 
	{ value: "B", numbers: [1,2,5], anotherValue: "C" }, 
]
```

### 1.2.0 - version update: 
using fieldPath with array indexes: still experimental and got bugs!
fieldPath: "-.firstArrayName[1].-.secondArrayName[3].-",

Feel free to contact me for suggestions, reporting bugs and etc.
dmik86@gmail.com

---
_Source: https://npm.io/package/data-manipulator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
