punyutility
v1.0.1
Published
Small size JavaScript utilities library
Downloads
7
Maintainers
Readme
Puny Utility v1.0.1
A simple library to make coding life easy. It is light weight & do not make your project heavy.
- Math
- String
- Number
- Date
- Object
- Array
- Utility
Documentation
In the documentation we will go through the utilities available in the library.
sum
Find sum on n numbers.
import { sum } from "punyutility";
const s = sum(1, 2, 3, 4); // 10
// or
const s = sum(...[1, 2, 3, 4]); // 10multiply
Find multiplication on n numbers.
import { multiply } from "punyutility";
const s = multiply(2, 3, 4); // 24
// or
const s = multiply(...[2, 3, 4]); // 24capitalize
Capitalize the 1st letter of the argument.
import { capitalize } from "punyutility";
const s = capitalize("my name is coder"); // My name is coderstartCase
Capitalize the 1st letter of each word in the argument.
import { startCase } from "punyutility";
const s = startCase("my name is coder"); // My Name Is CoderrandomNumber
Generate a random number within a given range from min to max.
import { randomNumber } from "punyutility";
/**
* @description pass the minimum & maximum.
*/
randomNumber(0, 10); // 10
randomNumber(0, 10); // 1
randomNumber(0, 10); // 4
/**
* @description if we pass a min value higher than max value. then the min value will be always the output.
*/
randomNumber(6, 0); // 6randomString
Generate a random string of length n.
import { randomString } from "punyutility";
randomString(10); // O1YrojMtjd
randomString(10); // wwJzClgolNtime
A simple native time object returns the date and time in 24 hours format. No extra 3rd party library.
time(new Date());Sample ouput:
{
day: 'Monday',
month: 'September',
date: 8,
year: 2025,
hours: 15,
minutes: 4,
seconds: 16
}Allowed argument types,
time("7-18-2024"); // mm-dd-yyyy
time("07-18-2024");
time("7/18/2024"); // mm/dd/yyyy
time("07/18/2024");
time(1757324387458); // milliseconds
time(new Date()); // dateThe output will be:
{
day: 'Thursday',
month: 'July',
date: 18,
year: 2024,
hours: 0,
minutes: 0,
seconds: 0
}detectFileType
Understand the file type by the base64 string. Helps render the file icon when rendering on the UI.
import { detectFileType } from "punyutility";
/**
* @description pass the base64 string as argument.
*/
detectFileType("iVBORw0KGgo..."); // pnghasAInB
Compares 2 objects and returns either true or false.
import { hasAInB } from "punyutility";
hasAInB({}, {}); // true
// or
hasAInB({ name: "Raju Das", age: 18 }, { name: "Raju Das" }); // false
// or
hasAInB({ name: "Raju Das", age: 18 }, { name: "Raju Das", age: 1 }) // false
// or
hasAInB({ name: "Raju Das", age: 18 }, { name: "Raju Das", age: 18 }) // truecloneDeep
The lightest code block for creating deep copy of objects & arrays.
import { cloneDeep } from "punyutility";
const itemA = {
name: "Raja",
address: {
city: "Kolkata",
state: "West Bengal"
},
getAddress() {
return `${this.address.city}, ${this.address.state}`;
}
};
const itemB = cloneDeep(itemA);
itemB.address.city = "Mumbai";
itemB.address.state = "Maharashtra";
itemA.getAddress(); // Kolkata, West Bengal
itemB.getAddress(); // Mumbai, Maharashtrafilter
Filter an array of objects.
import { filter } from "punyutility";
interface Student {
fName: string;
lName: string;
age: number;
subjects: string[];
};
const students: Student[] = [
{ fName: "Raju", lName: "Biswas", age: 14, subjects: ["Math", "Physics"] },
{ fName: "Swapna", lName: "Halder", age: 15, subjects: ["Math"] },
{ fName: "Surojit", lName: "Sen", age: 14, subjects: ["Physics"] },
];
filter(students, { age: 14 });Output will be:
[
{ fName: 'Surojit', lName: 'Sen', age: 14, subjects: [ 'Physics' ] }
]flatten
Flatten array elements. Supports multidimensional arrays.
import { flatten } from "punyutility";
type NumberMultiArray = (number | NumberMultiArray)[];
const itemsA: NumberMultiArray = [1, [2]]; // we can declare the items like this.
flatten(itemsA); // [1, 2]
const itemsB: NumberMultiArray = [1, [2], [[3], [4], [5, [6], [7], [8, 9, 10]], [10, 12]]];
flatten(itemsB);
/**
* Output:
* [
* 1, 2, 3, 4, 5,
* 6, 7, 8, 9, 10,
* 10, 12
* ]
*/Let's make it more complex by passing objects.
interface Student {
name: string;
roll: number;
class: number;
}
type StudentMultiArray = (Student | StudentMultiArray)[];
const students: StudentMultiArray = [
{ name: "Rahul", roll: 6, class: 9 },
{ name: "Ana", roll: 7, class: 9 },
[
{ name: "Sujoy", roll: 8, class: 9 },
{ name: "Purohit", roll: 9, class: 9 },
[
{ name: "Swapna", roll: 10, class: 9 },
[
{ name: "Rishav", roll: 11, class: 9 },
{ name: "Kushal", roll: 12, class: 9 },
{ name: "Sanjeev", roll: 13, class: 9 }
]
]
]
];
const flattenStudents: Student[] = flatten<Student>(students);The output will be,
[
{ name: 'Rahul', roll: 6, class: 9 },
{ name: 'Ana', roll: 7, class: 9 },
{ name: 'Sujoy', roll: 8, class: 9 },
{ name: 'Purohit', roll: 9, class: 9 },
{ name: 'Swapna', roll: 10, class: 9 },
{ name: 'Rishav', roll: 11, class: 9 },
{ name: 'Kushal', roll: 12, class: 9 },
{ name: 'Sanjeev', roll: 13, class: 9 }
]groupBy
A very powerful operation to arrage data under a certain group. Let's say we have a list of students,
interface Student {
fName: string;
lName: string;
roll: number;
class: number;
hasPickAndDrop: boolean;
}
const students: Student[] = [
{ fName: "Rahul", lName: "Mishra", roll: 6, class: 9, hasPickAndDrop: true },
{ fName: "Ana", lName: "Dutta", roll: 7, class: 9, hasPickAndDrop: true },
{ fName: "Sujoy", lName: "Mishra", roll: 8, class: 9, hasPickAndDrop: false },
{ fName: "Purohit", lName: "Saha", roll: 9, class: 10, hasPickAndDrop: true },
{ fName: "Swapna", lName: "Halder", roll: 10, class: 9, hasPickAndDrop: false },
{ fName: "Rishav", lName: "Sharma", roll: 11, class: 9, hasPickAndDrop: true },
{ fName: "Kushal", lName: "Paul", roll: 12, class: 10, hasPickAndDrop: true },
{ fName: "Sanjeev", lName: "Dutta", roll: 13, class: 10, hasPickAndDrop: true },
];We want to group the students by the hasPickAndDrop attribute,
const { groups, groupCollection } = groupBy<Student, "hasPickAndDrop">(students, "hasPickAndDrop");The groups will be a 2D array.
[
[true, [
{ fName: "Rahul", lName: "Mishra", roll: 6, class: 9, hasPickAndDrop: true },
{ fName: "Ana", lName: "Dutta", roll: 7, class: 9, hasPickAndDrop: true },
{ fName: "Purohit", lName: "Saha", roll: 9, class: 10, hasPickAndDrop: true },
{ fName: "Rishav", lName: "Sharma", roll: 11, class: 9, hasPickAndDrop: true },
{ fName: "Kushal", lName: "Paul", roll: 12, class: 10, hasPickAndDrop: true },
{ fName: "Sanjeev", lName: "Dutta", roll: 13, class: 10, hasPickAndDrop: true },
]],
[false, [
{ fName: "Sujoy", lName: "Mishra", roll: 8, class: 9, hasPickAndDrop: false },
{ fName: "Swapna", lName: "Halder", roll: 10, class: 9, hasPickAndDrop: false },
]]
]And, groupCollection will be a HashMap of booleans.
const groupCollection = new Map([
[true, [
{ fName: "Rahul", lName: "Mishra", roll: 6, class: 9, hasPickAndDrop: true },
{ fName: "Ana", lName: "Dutta", roll: 7, class: 9, hasPickAndDrop: true },
{ fName: "Purohit", lName: "Saha", roll: 9, class: 10, hasPickAndDrop: true },
{ fName: "Rishav", lName: "Sharma", roll: 11, class: 9, hasPickAndDrop: true },
{ fName: "Kushal", lName: "Paul", roll: 12, class: 10, hasPickAndDrop: true },
{ fName: "Sanjeev", lName: "Dutta", roll: 13, class: 10, hasPickAndDrop: true },
]],
[false, [
{ fName: "Sujoy", lName: "Mishra", roll: 8, class: 9, hasPickAndDrop: false },
{ fName: "Swapna", lName: "Halder", roll: 10, class: 9, hasPickAndDrop: false },
]]
]);
const whoDoNotNeedPickAndDrop: Student[] | undefined = groupCollection.get(false);List of students who do not need pick and drop.
[
{ fName: "Sujoy", lName: "Mishra", roll: 8, class: 9, hasPickAndDrop: false },
{ fName: "Swapna", lName: "Halder", roll: 10, class: 9, hasPickAndDrop: false },
]sortBy
Simple JavaScript Array.sort() function. It sorts an array of objects as per its datatypes.
Let's consider we have a list of students,
import { DateType, FieldType, SortOrder, sortBy } from "punyutility";
interface Student {
name: string;
roll: number;
marks: number;
hasOptionalSubject: boolean;
joiningDate: DateType;
}
const students: Student[] = [
{ name: "Ajay Sahu", roll: 10, marks: 86, hasOptionalSubject: true, joiningDate: "09/01/2025" },
{ name: "Suraj Verma", roll: 11, marks: 78, hasOptionalSubject: true, joiningDate: "09/02/2025" },
{ name: "Keya Sharma", roll: 12, marks: 91, hasOptionalSubject: true, joiningDate: new Date("08/30/2025") },
{ name: "Ankita Kundu", roll: 13, marks: 79, hasOptionalSubject: false, joiningDate: "08/27/2025" },
{ name: "Navin Reddy", roll: 14, marks: 82, hasOptionalSubject: false, joiningDate: "09-02-2025" },
{ name: "Lalith Choudhary", roll: 15, marks: 83, hasOptionalSubject: true, joiningDate: new Date("09-03-2025") }
];We will sort them by their marks (number), name (string), hasOptionalSubject (boolean) and joiningDate (DateType).
import { DateType, FieldType, SortOrder, sortBy } from "punyutility";
sortBy<Student, "marks">(students, "marks", FieldType.number, SortOrder.desc);
// or
sortBy<Student, "name">(students, "name", FieldType.string);
// or
sortBy(students, "hasOptionalSubject", FieldType.boolean, SortOrder.desc);
// or
sortBy(students, "joiningDate", FieldType.date, SortOrder.desc);Pay close attention to the arguments,
- When using TypeScript we can pass the
<Student, "joiningDate">. Which is optional. - 1st - The
list(required). - 2nd - A
fieldpresent in the list objects (required). - 3rd - The
typeof the field. Presented usingenum FieldType(required). - 4th - The
sorting order. Presented usingenum SortOrder. Default value isSortOrder.asc.
The output will look like Student[].
numericOrNull
A simple function that returns either numeric or null. To keep the consistency of the numeric datatype we can use numericOrNull.
import { numericOrNull } from "punyutility";
numericOrNull("abhisek"); // null
numericOrNull("10"); // 10
numericOrNull(9); // 9
numericOrNull("8.07"); // 8.07
numericOrNull(undefined); // nullPublish
Let's generate the build file,
npm run rollupIncrease the version count,
You must be logged in on npm. Then run,
npm publish --access privateIf publishing public plugins use,
npm publish --access public