Introduction to ntils
ntils is a powerful utility library for JavaScript that offers a wide range of functions aimed at making development easier and more efficient. With ntils, you can handle arrays, objects, strings, and other data types effortlessly. In this blog post, we will explore dozens of useful API explanations with code snippets to help you integrate ntils into your projects seamlessly.
Array Utilities
ntils.arrayChunk
Chunks an array into smaller arrays of a specified size.
const array = [1, 2, 3, 4, 5, 6, 7, 8];
const chunks = ntils.arrayChunk(array, 3);
console.log(chunks); // Outputs: [[1, 2, 3], [4, 5, 6], [7, 8]]
ntils.arrayUnique
Removes duplicate elements from an array.
const array = [1, 2, 2, 3, 4, 4, 5];
const uniqueArray = ntils.arrayUnique(array);
console.log(uniqueArray); // Outputs: [1, 2, 3, 4, 5]
Object Utilities
ntils.extend
Merges the properties of two or more objects into the first object.
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 };
const extendedObj = ntils.extend(obj1, obj2);
console.log(extendedObj); // Outputs: { a: 1, b: 3, c: 4 }
ntils.pick
Selects specified properties from an object.
const obj = { a: 1, b: 2, c: 3 };
const pickedObj = ntils.pick(obj, ['a', 'c']);
console.log(pickedObj); // Outputs: { a: 1, c: 3 }
String Utilities
ntils.capitalize
Capitalizes the first letter of a string.
const str = "hello world";
const capitalizedStr = ntils.capitalize(str);
console.log(capitalizedStr); // Outputs: "Hello world"
ntils.camelCase
Converts a string to camel case notation.
const str = "hello world";
const camelCaseStr = ntils.camelCase(str);
console.log(camelCaseStr); // Outputs: "helloWorld"
App Example Using ntils
Let’s create a simple app using some of the ntils APIs we have discussed.
// Importing ntils
const ntils = require('ntils');
// Sample data
const users = [
{ id: 1, name: 'Alice', age: 25 },
{ id: 2, name: 'Bob', age: 30 },
{ id: 3, name: 'Charlie', age: 35 }
];
// Utility functions
function getUserNames(users) {
return users.map(user => ntils.capitalize(user.name));
}
function getUserById(users, id) {
return ntils.pick(users.find(user => user.id === id), ['id', 'name']);
}
// Usage
console.log(getUserNames(users)); // Outputs: ["Alice", "Bob", "Charlie"]
console.log(getUserById(users, 2)); // Outputs: { id: 2, name: "Bob" }
In this example, we demonstrate how to collect user names and fetch specific user details by employing ntils functions. This helps in significantly reducing code complexity and increasing readability.
Hash: a4c75c65b741dd8f65ceb842da8c8d8f5a854ec9a6982f01fcf9a157b6a6e8ab