This is a simple utility package that provides functions for text formatting and array sorting. It demonstrates how to create and publish an npm package, and leverages the powerful Lodash library for text formatting and sorting tasks.
npm install dzamiy-npmcapitalizeWords(text): Takes a string as input and capitalizes the first letter of each word. This can be useful for formatting titles or headers.
capitalizeWords(text):接受一个字符串作为输入,并将每个单词的首字母大写。这对于格式化标题或头部非常有用。
sortArray(arr): Takes an array and sorts its elements in ascending order. This can be used to sort numbers or strings.
sortArray(arr):接受一个数组并将其元素按升序排列。可以用来对数字或字符串进行排序。
bash
npm install dzamiy-npm
`
Usage / 使用方法
Once installed, you can use the package in your JavaScript code. Here’s how:
安装后,您可以在 JavaScript 代码中使用此包。使用方法如下:
Import the package / 导入包
`javascript
import MyUtility from 'dzamiy-npm';
`
Example 1: Capitalize Words / 示例 1:首字母大写
The capitalizeWords function capitalizes the first letter of each word in a sentence. This is particularly useful for formatting titles or strings where each word needs to start with an uppercase letter.
capitalizeWords 函数将句子中每个单词的首字母转换为大写。这在格式化标题或需要将每个单词的首字母大写的字符串时特别有用。
`javascript
const formattedText = MyUtility.capitalizeWords('hello world');
console.log(formattedText); // Output: "Hello World"
`
Example 2: Sort an Array / 示例 2:排序数组
The sortArray function sorts an array of numbers or strings in ascending order.
sortArray 函数将一个数字或字符串数组按升序排序。
`javascript
const sortedArray = MyUtility.sortArray([5, 3, 8, 1]);
console.log(sortedArray); // Output: [1, 3, 5, 8]
``