npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

data-manipulator

v1.2.4

Published

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

Downloads

43

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. [email protected]