ngx-core-utility
v1.0.1
Published
A collection of utilities for Angular applications including enums, constants, string, object, and array utilities.
Downloads
8
Maintainers
Readme
ngx-core-utility
ngx-core-utility is a utility library for Angular that provides commonly used enums, constants, and utility methods for arrays, strings, and objects. This package helps you improve the efficiency and readability of your Angular code.
Installation
To install the package, run the following command in your Angular project directory:
npm install ngx-core-utilityUsage
1. Import the Package
In your Angular components, services, or any other file, you can import the utilities like this:
import { HttpStatusCode, HttpMethod } from 'ngx-core-utility';
import { AUTH_LABELS } from 'ngx-core-utility';
import { ArrayUtil, StringUtil, ObjectUtil } from 'ngx-core-utility';2. Use the Enums, Constants, & Utility Methods
Example 1: Using Enums
You can use the enums like HttpStatusCode and HttpMethod to manage HTTP status codes and methods:
// Example GET request
getPosts(): Observable<any> {
return this.makeRequest<any>(HttpMethod.GET, 'posts');
}
// Example POST request
createPost(postData: any): Observable<any> {
return this.makeRequest<any>(HttpMethod.POST, 'posts', postData);
}
// Example PUT request
updatePost(postId: number, postData: any): Observable<any> {
return this.makeRequest<any>(HttpMethod.PUT, `posts/${postId}`, postData);
}
// Example DELETE request
deletePost(postId: number): Observable<any> {
return this.makeRequest<any>(HttpMethod.DELETE, `posts/${postId}`);
}
private handleError(error: HttpErrorResponse) {
let errorMessage = '';
if (error.status === HttpStatusCode.BAD_REQUEST) {
errorMessage = 'Bad request. Please check the input data.';
} else if (error.status === HttpStatusCode.NOT_FOUND) {
errorMessage = 'The requested resource was not found.';
} else if (error.status === HttpStatusCode.INTERNAL_SERVER_ERROR) {
errorMessage = 'Internal server error. Please try again later.';
} else {
errorMessage = 'An unknown error occurred.';
}
return throwError(errorMessage);
}
Example 2: Using Constants
Constants like AUTH_LABELS can help you standardize common labels:
// login.component.ts
import { Component } from '@angular/core';
import { AUTH_LABELS } from './auth-labels.constants';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
})
export class LoginComponent {
labels = AUTH_LABELS;
}
<!-- login.component.html -->
<form>
<label for="username">{{ labels.USERNAME }}</label>
<input type="text" id="username" name="username" required />
<label for="password">{{ labels.PASSWORD }}</label>
<input type="password" id="password" name="password" required />
<button type="submit">{{ labels.LOGIN }}</button>
</form>
<p><a href="#">{{ labels.SIGNUP }}</a></p>
Example 3: Using Array Utilities
The groupBy method allows you to group an array by a specific property:
const users = [
{ id: 1, role: 'admin' },
{ id: 2, role: 'user' },
{ id: 3, role: 'admin' },
];
const groupedUsers = ArrayUtil.groupBy(users, 'role');
console.log(groupedUsers);
// Output:
// {
// admin: [{ id: 1, role: 'admin' }, { id: 3, role: 'admin' }],
// user: [{ id: 2, role: 'user' }]
// }Example 4: Using String Utilities
You can validate if a string is valid JSON using isValidJSON:
const jsonStr = '{"name":"John", "age":30}';
console.log(StringUtil.isValidJSON(jsonStr)); // Output: trueYou can also convert numbers to words using amountToWords:
console.log(StringUtil.amountToWords(123)); // Output: 'One Hundred Twenty Three'Example 5: Using Object Utilities
Find keys with null or undefined values in an object:
const user = { name: 'John', email: null, age: undefined, address: 'New York' };
const nullUndefinedKeys = ObjectUtil.findNullUndefinedKeys(user);
console.log(nullUndefinedKeys); // Output: ['email', 'age']Convert an object to a query string:
const queryParams = { name: 'John', age: 30, city: 'New York' };
console.log(ObjectUtil.objectToQueryString(queryParams));
// Output: 'name=John&age=30&city=New%20York'License
This project is licensed under the MIT License - see the LICENSE file for details.
