430 lines
		
	
	
		
			9.5 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			430 lines
		
	
	
		
			9.5 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
| [@resultium/utils - v1.0.0-alpha.0](README.md) / Exports
 | |
| 
 | |
| # @resultium/utils - v1.0.0-alpha.0
 | |
| 
 | |
| ## Table of contents
 | |
| 
 | |
| ### Functions
 | |
| 
 | |
| - [capitalizeFirstChar](modules.md#capitalizefirstchar)
 | |
| - [classNames](modules.md#classnames)
 | |
| - [conditionalJoin](modules.md#conditionaljoin)
 | |
| - [extractTextFromHTML](modules.md#extracttextfromhtml)
 | |
| - [isDeepWeakEqual](modules.md#isdeepweakequal)
 | |
| - [randomInRange](modules.md#randominrange)
 | |
| - [removeFromArrayByKeyValue](modules.md#removefromarraybykeyvalue)
 | |
| - [removeObjectProperty](modules.md#removeobjectproperty)
 | |
| - [replaceFromArrayByKeyValue](modules.md#replacefromarraybykeyvalue)
 | |
| - [splitIntoChunks](modules.md#splitintochunks)
 | |
| 
 | |
| ## Functions
 | |
| 
 | |
| ### capitalizeFirstChar
 | |
| 
 | |
| ▸ **capitalizeFirstChar**(`string`): `string`
 | |
| 
 | |
| Capitalizes first character of the given string
 | |
| 
 | |
| #### Parameters
 | |
| 
 | |
| | Name | Type | Description |
 | |
| | :------ | :------ | :------ |
 | |
| | `string` | `string` | string to modify |
 | |
| 
 | |
| #### Returns
 | |
| 
 | |
| `string`
 | |
| 
 | |
| new string
 | |
| 
 | |
| **`Example`**
 | |
| 
 | |
| ```ts
 | |
| // Prints "Lorem ipsum"
 | |
| console.log(capitalizeFirstChar("lorem ipsum"))
 | |
| ```
 | |
| 
 | |
| #### Defined in
 | |
| 
 | |
| [functions/capitalizeFirstChar.ts:32](https://git.resultium.net/public/utils/src/commit/27db7abb6a030b268712e1f2f40cdb7073978c94/src/functions/capitalizeFirstChar.ts#L32)
 | |
| 
 | |
| ___
 | |
| 
 | |
| ### classNames
 | |
| 
 | |
| ▸ **classNames**(`...classes`): `string`
 | |
| 
 | |
| Conditionally joins given class strings
 | |
| 
 | |
| #### Parameters
 | |
| 
 | |
| | Name | Type | Description |
 | |
| | :------ | :------ | :------ |
 | |
| | `...classes` | `string`[] | class strings to join |
 | |
| 
 | |
| #### Returns
 | |
| 
 | |
| `string`
 | |
| 
 | |
| new string
 | |
| 
 | |
| **`Example`**
 | |
| 
 | |
| ```ts
 | |
| let isVisible = false
 | |
| 
 | |
| // prints "bg-blue-300 hidden"
 | |
| console.log(classNames("bg-blue-300", isVisible ? "block" : "hidden"))
 | |
| ```
 | |
| 
 | |
| #### Defined in
 | |
| 
 | |
| [functions/classNames.ts:34](https://git.resultium.net/public/utils/src/commit/27db7abb6a030b268712e1f2f40cdb7073978c94/src/functions/classNames.ts#L34)
 | |
| 
 | |
| ___
 | |
| 
 | |
| ### conditionalJoin
 | |
| 
 | |
| ▸ **conditionalJoin**(`array`, `joinChar`): `string`
 | |
| 
 | |
| Conditionally joins given strings
 | |
| 
 | |
| #### Parameters
 | |
| 
 | |
| | Name | Type | Description |
 | |
| | :------ | :------ | :------ |
 | |
| | `array` | `string`[] | strings to join |
 | |
| | `joinChar` | `string` | character used to join strings |
 | |
| 
 | |
| #### Returns
 | |
| 
 | |
| `string`
 | |
| 
 | |
| new string
 | |
| 
 | |
| **`Example`**
 | |
| 
 | |
| ```ts
 | |
| let isAdmin = true
 | |
| 
 | |
| // prints "Hello, Administrator!"
 | |
| console.log(conditionalJoin(["Hello,", isAdmin ? "Administrator!" : "User!"], " "))
 | |
| ```
 | |
| 
 | |
| #### Defined in
 | |
| 
 | |
| [functions/conditionalJoin.ts:35](https://git.resultium.net/public/utils/src/commit/27db7abb6a030b268712e1f2f40cdb7073978c94/src/functions/conditionalJoin.ts#L35)
 | |
| 
 | |
| ___
 | |
| 
 | |
| ### extractTextFromHTML
 | |
| 
 | |
| ▸ **extractTextFromHTML**(`html`, `domParser?`): ``null`` \| `string`
 | |
| 
 | |
| Extracts text content from an HTML string
 | |
| 
 | |
| #### Parameters
 | |
| 
 | |
| | Name | Type | Description |
 | |
| | :------ | :------ | :------ |
 | |
| | `html` | `string` | HTML to extract text from |
 | |
| | `domParser` | `DOMParser` | DOMParser to use |
 | |
| 
 | |
| #### Returns
 | |
| 
 | |
| ``null`` \| `string`
 | |
| 
 | |
| **`Remarks`**
 | |
| 
 | |
| By default this function uses `new window.DOMParser()` to parse HTML, however
 | |
| on the server you might need to use alternative DOM Parser such as JSDOM,
 | |
| puppeteer or cheerio as `window` object is not defined in NodeJS
 | |
| 
 | |
| **`See`**
 | |
| 
 | |
| [Why doesn't Node.js have a native DOM?](https://stackoverflow.com/q/32723111/14544732)
 | |
| for more information on why this function is made the way it is
 | |
| 
 | |
| **`Example`**
 | |
| 
 | |
| Here's how you would use this function in browser
 | |
| ```ts
 | |
| let HTML = '<p><a>Lorem ipsum</a> dolor sit</p>'
 | |
| 
 | |
| // prints "Lorem ipsum dolor sit"
 | |
| console.log(extractTextFromHTML(HTML))
 | |
| ```
 | |
| 
 | |
| **`Example`**
 | |
| 
 | |
| Here's how you would use this function on server
 | |
| ```ts
 | |
| import { JSDOM } from "jsdom";
 | |
| const domParser = new new JSDOM().window.DOMParser();
 | |
| 
 | |
| let HTML = '<p><a>Lorem ipsum</a> dolor sit</p>'
 | |
| 
 | |
| // prints "Lorem ipsum dolor sit"
 | |
| console.log(extractTextFromHTML(HTML, domParser))
 | |
| ```
 | |
| 
 | |
| #### Defined in
 | |
| 
 | |
| [functions/extractTextFromHTML.ts:56](https://git.resultium.net/public/utils/src/commit/27db7abb6a030b268712e1f2f40cdb7073978c94/src/functions/extractTextFromHTML.ts#L56)
 | |
| 
 | |
| ___
 | |
| 
 | |
| ### isDeepWeakEqual
 | |
| 
 | |
| ▸ **isDeepWeakEqual**(`obj1`, `obj2`): `boolean`
 | |
| 
 | |
| Weakly compares two objects
 | |
| 
 | |
| #### Parameters
 | |
| 
 | |
| | Name | Type | Description |
 | |
| | :------ | :------ | :------ |
 | |
| | `obj1` | `object` | first object for comparison |
 | |
| | `obj2` | `object` | second object for comparison |
 | |
| 
 | |
| #### Returns
 | |
| 
 | |
| `boolean`
 | |
| 
 | |
| true if equal
 | |
| 
 | |
| **`Remarks`**
 | |
| 
 | |
| uses JSON.stringify()
 | |
| 
 | |
| **`Example`**
 | |
| 
 | |
| ```ts
 | |
| // prints false
 | |
| console.log(isDeepWeakEqual({ a: 1 }, ["lorem"]))
 | |
| 
 | |
| // prints true
 | |
| console.log(isDeepWeakEqual({ a: 1 }, { a: 1 }))
 | |
| 
 | |
| // prints true
 | |
| // snippet displaying the weak equality of this function
 | |
| console.log(
 | |
|   isDeepWeakEqual(
 | |
|     { date: "2024-03-12T18:19:50.548Z" },
 | |
|     { date: new Date(1710267590548) },
 | |
|   ),
 | |
| );
 | |
| ```
 | |
| 
 | |
| #### Defined in
 | |
| 
 | |
| [functions/isDeepWeakEqual.ts:47](https://git.resultium.net/public/utils/src/commit/27db7abb6a030b268712e1f2f40cdb7073978c94/src/functions/isDeepWeakEqual.ts#L47)
 | |
| 
 | |
| ___
 | |
| 
 | |
| ### randomInRange
 | |
| 
 | |
| ▸ **randomInRange**(`min`, `max`): `number`
 | |
| 
 | |
| Returns a pseudo-random number in given range
 | |
| 
 | |
| #### Parameters
 | |
| 
 | |
| | Name | Type | Description |
 | |
| | :------ | :------ | :------ |
 | |
| | `min` | `number` | minimum acceptable value |
 | |
| | `max` | `number` | maximum acceptable value |
 | |
| 
 | |
| #### Returns
 | |
| 
 | |
| `number`
 | |
| 
 | |
| number [min; max]
 | |
| 
 | |
| **`Remarks`**
 | |
| 
 | |
| Uses Math.random(), which is not cryptographically secure
 | |
| Returns only integers
 | |
| 
 | |
| **`Example`**
 | |
| 
 | |
| ```ts
 | |
| // prints 66
 | |
| console.log(randomInRange(1, 100))
 | |
| ```
 | |
| 
 | |
| #### Defined in
 | |
| 
 | |
| [functions/randomInRange.ts:37](https://git.resultium.net/public/utils/src/commit/27db7abb6a030b268712e1f2f40cdb7073978c94/src/functions/randomInRange.ts#L37)
 | |
| 
 | |
| ___
 | |
| 
 | |
| ### removeFromArrayByKeyValue
 | |
| 
 | |
| ▸ **removeFromArrayByKeyValue**\<`T`\>(`array`, `keyValue`): `T`[]
 | |
| 
 | |
| Removes an object from an array by key value object.
 | |
| 
 | |
| #### Type parameters
 | |
| 
 | |
| | Name |
 | |
| | :------ |
 | |
| | `T` |
 | |
| 
 | |
| #### Parameters
 | |
| 
 | |
| | Name | Type | Description |
 | |
| | :------ | :------ | :------ |
 | |
| | `array` | `T`[] | array which shall be manipulated with |
 | |
| | `keyValue` | `Partial`\<\{ [K in string \| number \| symbol]: T[K] }\> | key-value object which defines which object shall be removed from the array |
 | |
| 
 | |
| #### Returns
 | |
| 
 | |
| `T`[]
 | |
| 
 | |
| modified array
 | |
| 
 | |
| **`Remarks`**
 | |
| 
 | |
| This method doesn't support deep comparements.
 | |
| Vulnerable to generic object injection sink
 | |
| 
 | |
| **`Example`**
 | |
| 
 | |
| ```ts
 | |
| // prints [{ a: "1", b: "0" }, { a: "2", b: "0" }]
 | |
| console.log(removeFromArrayByKeyValue([{ a: "1", b: "0" }, { a: "2", b: "0" }, { a: "3", b: "0" }], { a: "3" }))
 | |
| 
 | |
| // prints []
 | |
| console.log(removeFromArrayByKeyValue([{ a: "1", b: "0" }, { a: "2", b: "0" }, { a: "3", b: "0" }], { b: "0" }))
 | |
| ```
 | |
| 
 | |
| #### Defined in
 | |
| 
 | |
| [functions/removeFromArrayByKeyValue.ts:40](https://git.resultium.net/public/utils/src/commit/27db7abb6a030b268712e1f2f40cdb7073978c94/src/functions/removeFromArrayByKeyValue.ts#L40)
 | |
| 
 | |
| ___
 | |
| 
 | |
| ### removeObjectProperty
 | |
| 
 | |
| ▸ **removeObjectProperty**\<`T`, `K`\>(`object`, `key`): `Omit`\<`T`, `K`\>
 | |
| 
 | |
| Removes an object property by provided key
 | |
| 
 | |
| #### Type parameters
 | |
| 
 | |
| | Name | Type |
 | |
| | :------ | :------ |
 | |
| | `T` | `T` |
 | |
| | `K` | extends `string` \| `number` \| `symbol` |
 | |
| 
 | |
| #### Parameters
 | |
| 
 | |
| | Name | Type | Description |
 | |
| | :------ | :------ | :------ |
 | |
| | `object` | `T` | object which shall be manipulated |
 | |
| | `key` | `K` | key which should be used to remove a property from object |
 | |
| 
 | |
| #### Returns
 | |
| 
 | |
| `Omit`\<`T`, `K`\>
 | |
| 
 | |
| new object
 | |
| 
 | |
| **`Example`**
 | |
| 
 | |
| ```ts
 | |
| // print { a: "1", b: "2" }
 | |
| console.log(removeObjectProperty({ a: "1", b: "2", c: "3" }, "c"))
 | |
| ```
 | |
| 
 | |
| #### Defined in
 | |
| 
 | |
| [functions/removeObjectProperty.ts:33](https://git.resultium.net/public/utils/src/commit/27db7abb6a030b268712e1f2f40cdb7073978c94/src/functions/removeObjectProperty.ts#L33)
 | |
| 
 | |
| ___
 | |
| 
 | |
| ### replaceFromArrayByKeyValue
 | |
| 
 | |
| ▸ **replaceFromArrayByKeyValue**\<`T`\>(`array`, `keyValue`, `newObject`): `T`[]
 | |
| 
 | |
| Replaces an object from an array with key value object search
 | |
| 
 | |
| #### Type parameters
 | |
| 
 | |
| | Name |
 | |
| | :------ |
 | |
| | `T` |
 | |
| 
 | |
| #### Parameters
 | |
| 
 | |
| | Name | Type | Description |
 | |
| | :------ | :------ | :------ |
 | |
| | `array` | `T`[] | array which shall be manipulated with |
 | |
| | `keyValue` | `Partial`\<\{ [K in string \| number \| symbol]: T[K] }\> | key-value object which defines which object shall be removed from the array |
 | |
| | `newObject` | `Partial`\<\{ [K in string \| number \| symbol]: T[K] }\> | object or values to replace within the searched object |
 | |
| 
 | |
| #### Returns
 | |
| 
 | |
| `T`[]
 | |
| 
 | |
| modified array
 | |
| 
 | |
| **`Remarks`**
 | |
| 
 | |
| This method doesn't support deep comparements.
 | |
| Vulnerable to generic object injection sink
 | |
| 
 | |
| **`Example`**
 | |
| 
 | |
| ```ts
 | |
| // prints [{ a: "1", b: "0" }, { a: "2", b: "0" }, { a: "3", b: "lorem" }]
 | |
| console.log(replaceFromArrayByKeyValue([{ a: "1", b: "0" }, { a: "2", b: "0" }, { a: "3", b: "0" }], { a: "3" }, { b: "lorem" }))
 | |
| 
 | |
| // prints [{ a: "lorem", b: "0" }, { a: "2", b: "0" }, { a: "3", b: "0" }]
 | |
| // replaces only the first occurance
 | |
| console.log(replaceFromArrayByKeyValue([{ a: "1", b: "0" }, { a: "2", b: "0" }, { a: "3", b: "0" }], { b: "0" }, { a: "lorem" }))
 | |
| ```
 | |
| 
 | |
| #### Defined in
 | |
| 
 | |
| [functions/replaceFromArrayByKeyValue.ts:42](https://git.resultium.net/public/utils/src/commit/27db7abb6a030b268712e1f2f40cdb7073978c94/src/functions/replaceFromArrayByKeyValue.ts#L42)
 | |
| 
 | |
| ___
 | |
| 
 | |
| ### splitIntoChunks
 | |
| 
 | |
| ▸ **splitIntoChunks**\<`T`\>(`array`, `chunkSize`): `T`[][]
 | |
| 
 | |
| Splits an array into chunks
 | |
| 
 | |
| #### Type parameters
 | |
| 
 | |
| | Name |
 | |
| | :------ |
 | |
| | `T` |
 | |
| 
 | |
| #### Parameters
 | |
| 
 | |
| | Name | Type | Description |
 | |
| | :------ | :------ | :------ |
 | |
| | `array` | `T`[] | array which shall be split into chunks |
 | |
| | `chunkSize` | `number` | size of a single chunk |
 | |
| 
 | |
| #### Returns
 | |
| 
 | |
| `T`[][]
 | |
| 
 | |
| array of chunks
 | |
| 
 | |
| **`Example`**
 | |
| 
 | |
| ```ts
 | |
| // prints [["a", "b", "c"], ["d", "e", "f"]]
 | |
| console.log(splitIntoChunks(["a", "b", "c", "d", "e", "f"], 3));
 | |
| ```
 | |
| 
 | |
| #### Defined in
 | |
| 
 | |
| [functions/splitIntoChunks.ts:33](https://git.resultium.net/public/utils/src/commit/27db7abb6a030b268712e1f2f40cdb7073978c94/src/functions/splitIntoChunks.ts#L33)
 |