Added kebabCase, typedocs and some test fixes

This commit is contained in:
Martin Donnelly 2022-10-21 12:08:36 +01:00
parent 22d103dc62
commit 068c696f55
83 changed files with 7257 additions and 4882 deletions

0
.editorconfig Normal file → Executable file
View File

0
.eslintrc.json Normal file → Executable file
View File

0
.gitignore vendored Normal file → Executable file
View File

0
.prettierrc Normal file → Executable file
View File

0
.travis.yml Normal file → Executable file
View File

1
docs/.nojekyll Normal file
View File

@ -0,0 +1 @@
TypeDoc added this file to prevent GitHub Pages from using Jekyll. You can turn off this behavior by setting the `githubPages` option to false.

7
docs/README.md Normal file
View File

@ -0,0 +1,7 @@
@rakh/utils / [Exports](modules.md)
# Rakh/utils
A library of functions that I've been using over the years.
Documentation [here](./docs/modules.md)

61
docs/classes/LatLong.md Normal file
View File

@ -0,0 +1,61 @@
[@rakh/utils](../README.md) / [Exports](../modules.md) / LatLong
# Class: LatLong
Calculate the distance between two latLongs
**`Param`**
**`Param`**
**`Param`**
**`Param`**
## Table of contents
### Constructors
- [constructor](LatLong.md#constructor)
### Properties
- [lat](LatLong.md#lat)
- [long](LatLong.md#long)
## Constructors
### constructor
**new LatLong**(`lat`, `long`)
#### Parameters
| Name | Type |
| :------ | :------ |
| `lat` | `number` |
| `long` | `number` |
#### Defined in
latLong.ts:12
## Properties
### lat
**lat**: `number`
#### Defined in
latLong.ts:10
___
### long
**long**: `number`
#### Defined in
latLong.ts:11

View File

@ -0,0 +1,178 @@
[@rakh/utils](../README.md) / [Exports](../modules.md) / limitedArray
# Class: limitedArray
A finite array, which is can be limited in size, items added to the end push items off the top.
## Table of contents
### Constructors
- [constructor](limitedArray.md#constructor)
### Properties
- [\_array](limitedArray.md#_array)
- [\_limit](limitedArray.md#_limit)
### Methods
- [add](limitedArray.md#add)
- [get](limitedArray.md#get)
- [length](limitedArray.md#length)
- [limit](limitedArray.md#limit)
- [push](limitedArray.md#push)
- [shift](limitedArray.md#shift)
## Constructors
### constructor
**new limitedArray**(`size?`)
Used to construct the LimitedArray
Defaults to 100 when no size is passed in
#### Parameters
| Name | Type | Default value |
| :------ | :------ | :------ |
| `size` | `number` | `100` |
#### Defined in
limitedArray.ts:14
## Properties
### \_array
**\_array**: `any`[]
#### Defined in
limitedArray.ts:6
___
### \_limit
**\_limit**: `number`
#### Defined in
limitedArray.ts:7
## Methods
### add
**add**(`items`): `void`
Bulk add an array to the array
#### Parameters
| Name | Type |
| :------ | :------ |
| `items` | `any`[] |
#### Returns
`void`
#### Defined in
limitedArray.ts:58
___
### get
**get**(): `any`[]
Return the items within the array
#### Returns
`any`[]
#### Defined in
limitedArray.ts:33
___
### length
**length**(): `number`
Returns the length of the array
#### Returns
`number`
#### Defined in
limitedArray.ts:50
___
### limit
**limit**(`size`): `void`
Limits the size of the array
#### Parameters
| Name | Type |
| :------ | :------ |
| `size` | `number` |
#### Returns
`void`
#### Defined in
limitedArray.ts:41
___
### push
**push**(`item`): `void`
Add items to the end of an array
#### Parameters
| Name | Type |
| :------ | :------ |
| `item` | `any` |
#### Returns
`void`
#### Defined in
limitedArray.ts:24
___
### shift
**shift**(): `any`
Remove an item from the beginning of an array
#### Returns
`any`
#### Defined in
limitedArray.ts:68

551
docs/modules.md Normal file
View File

@ -0,0 +1,551 @@
[@rakh/utils](README.md) / Exports
# @rakh/utils
## Table of contents
### Classes
- [LatLong](classes/LatLong.md)
- [limitedArray](classes/limitedArray.md)
### Variables
- [LocalStorage](modules.md#localstorage)
### Functions
- [arrayFromObj](modules.md#arrayfromobj)
- [debounce](modules.md#debounce)
- [distance](modules.md#distance)
- [extractFromObj](modules.md#extractfromobj)
- [get](modules.md#get)
- [getDays](modules.md#getdays)
- [hasOwn](modules.md#hasown)
- [hourFloor](modules.md#hourfloor)
- [isEmpty](modules.md#isempty)
- [kebabCase](modules.md#kebabcase)
- [maybePluralize](modules.md#maybepluralize)
- [minuteFloor](modules.md#minutefloor)
- [once](modules.md#once)
- [partOfDay](modules.md#partofday)
- [throttle](modules.md#throttle)
- [toHour](modules.md#tohour)
## Variables
### LocalStorage
**LocalStorage**: `Object` = `_LocalStorage`
#### Defined in
LocalStorage.ts:44
## Functions
### arrayFromObj
**arrayFromObj**(`jsonObj`, `wantedFields`): `any`[]
Create an array from an Object using specified fields
**`Example`**
```ts
U.arrayFromObj({ a: 1, b: 2 }, ['a', 'b']) // => [1, 2]
```
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `jsonObj` | `Object` | The Original object |
| `wantedFields` | `string`[] | The required fields |
#### Returns
`any`[]
#### Defined in
arrayFromObj.ts:9
___
### debounce
**debounce**(`fn`, `time`): `Function`
Debounce the calling of a function
**`Signature`**
U.debounce(fn, time)
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `fn` | `Function` | The function to be debounced |
| `time` | `number` | How long to wait |
#### Returns
`Function`
#### Defined in
debounce.ts:12
___
### distance
**distance**(`lat1`, `lon1`, `lat2`, `lon2`): `number`
Calculate the distance between two lat long points
**`Signature`**
U.distance(lat1, long1, lat2, long2)
**`Example`**
```ts
distance(1, 1, 2, 2) // => 157.22543203805722;
```
#### Parameters
| Name | Type |
| :------ | :------ |
| `lat1` | `number` |
| `lon1` | `number` |
| `lat2` | `number` |
| `lon2` | `number` |
#### Returns
`number`
#### Defined in
distance.ts:16
**distance**(`latLong1`, `latLong2`): `number`
Calculate the distance between two lat long points
**`Signature`**
U.distance(latLong1, latLong2)
**`Example`**
```ts
const a: U.LatLong = new LatLong(1, 1);
const b: U.LatLong = new LatLong(2, 2);
U.distance(a, b) // => 157.22543203805722
```
#### Parameters
| Name | Type |
| :------ | :------ |
| `latLong1` | [`LatLong`](classes/LatLong.md) |
| `latLong2` | [`LatLong`](classes/LatLong.md) |
#### Returns
`number`
#### Defined in
distance.ts:33
___
### extractFromObj
**extractFromObj**(`jsonObj`, `wantedFields`): `Object`
Extract an object from another object using specific fields
**`Signature`**
U.extractFromObj(jsonObj, wantedFields)
**`Example`**
```ts
U.extractFromObj({ a: 1, b: 2 }, ['a']) // => { a: 1 }
```
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `jsonObj` | `Object` | The source object |
| `wantedFields` | `string`[] | The required fields |
#### Returns
`Object`
#### Defined in
extractFromObj.ts:12
___
### get
**get**(`obj`, `path`, `defaultValue?`): `any`
Get the value of an item inside an object
**`Example`**
```ts
U.get({ a: 1, b: 2 }, 'b') // => 2
```
#### Parameters
| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `obj` | `Object` | `undefined` | The source object |
| `path` | `string` | `undefined` | The path to the object |
| `defaultValue` | `any` | `undefined` | A default value to be returned |
#### Returns
`any`
#### Defined in
get.ts:10
___
### getDays
**getDays**(`startdate`, `enddate`): `number`
Get number of days between two Date objects
#### Parameters
| Name | Type |
| :------ | :------ |
| `startdate` | `Date` |
| `enddate` | `Date` |
#### Returns
`number`
#### Defined in
getDays.ts:7
___
### hasOwn
**hasOwn**(`obj`, `prop`): `boolean`
Check if an object has an property
**`Example`**
```ts
U.hasOwn({bob:'1'}, 'bob') // => true
```
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `obj` | `Object` | The source object |
| `prop` | `string` | The required property |
#### Returns
`boolean`
#### Defined in
hasOwn.ts:12
___
### hourFloor
**hourFloor**(`timestamp`): `string`
Get the hour floor as a Base 32 string
**`Example`**
```ts
U.hourFloor(1605532173) // => '1fnp540'
```
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `timestamp` | `number` | The timestamp as a number |
#### Returns
`string`
#### Defined in
hourFloor.ts:10
___
### isEmpty
**isEmpty**(`obj`): `boolean`
Check if an object is empty
**`Example`**
```ts
U.isEmpty({}) // => true
U.isEmpty({ bob: true }) // => false
```
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `obj` | `Object` | The object being checked |
#### Returns
`boolean`
#### Defined in
isEmpty.ts:9
___
### kebabCase
**kebabCase**(`inval`): `string`
Turn a string into a kebab-case string
**`Example`**
```ts
U.kebabCase('test string') // => 'test-string'
U.kebabCase('testString') // => 'test-string'
U.kebabCase('test_string') // => 'test-string'
```
#### Parameters
| Name | Type |
| :------ | :------ |
| `inval` | ``null`` \| `string` |
#### Returns
`string`
#### Defined in
kebabCase.ts:10
___
### maybePluralize
**maybePluralize**(`count`, `noun`, `suffix?`): `string`
Maybe pluralize a count:
**`Signature`**
U.maybePluralize(number, noun, suffix)
**`Example`**
```ts
U.maybePluralize(1, 'Bag', 's') // => 1 Bag
U.maybePluralize(5, 'Bag', 's') // => 5 Bags
```
#### Parameters
| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `count` | `number` | `undefined` | the value counting |
| `noun` | `string` | `undefined` | the name of the value |
| `suffix` | `string` | `'s'` | the suffix |
#### Returns
`string`
#### Defined in
maybePluralize.ts:16
___
### minuteFloor
**minuteFloor**(`timestamp?`): `string`
Get the minute floor as a Base 32 string
**`Example`**
```ts
U.minuteFloor(1605532173) // => '1fnp540'
```
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `timestamp?` | ``null`` \| `number` | The timestamp as a number |
#### Returns
`string`
#### Defined in
minuteFloor.ts:10
___
### once
**once**(`fn`): `Function`
Trigger a function once and then prevent it from triggering again
**`Signature`**
U.once(fn)
#### Parameters
| Name | Type |
| :------ | :------ |
| `fn` | `Function` |
#### Returns
`Function`
#### Defined in
once.ts:9
___
### partOfDay
**partOfDay**(`timeString`, `today?`): `string`
Get a string phrase for the current time of day
**`Signature`**
U.partOfDay(timeString, today)
**`Example`**
```ts
U.partOfDay('13:00') // => 'Afternoon'
```
#### Parameters
| Name | Type | Default value |
| :------ | :------ | :------ |
| `timeString` | ``null`` \| `string` | `undefined` |
| `today` | `boolean` | `false` |
#### Returns
`string`
#### Defined in
partOfDay.ts:11
___
### throttle
**throttle**(`callback`, `limit`): `Function`
Throttle the calling of a function
**`Signature`**
U.throttle(callback, limit)
#### Parameters
| Name | Type |
| :------ | :------ |
| `callback` | `Function` |
| `limit` | `number` |
#### Returns
`Function`
#### Defined in
throttle.ts:10
___
### toHour
**toHour**(`currentTimsestamp`, `extra?`): `number`
Return the number of Milliseconds to the hour for the supplied timestamp
**`Signature`**
U.toHour(currentTimsestamp, extra)
**`Example`**
```ts
U.toHour('13:00') // => 1605532173
```
#### Parameters
| Name | Type | Default value |
| :------ | :------ | :------ |
| `currentTimsestamp` | `number` | `undefined` |
| `extra` | `number` | `0` |
#### Returns
`number`
*
#### Defined in
toHour.ts:12

0
out.json Normal file → Executable file
View File

11191
package-lock.json generated Normal file → Executable file

File diff suppressed because it is too large Load Diff

22
package.json Normal file → Executable file
View File

@ -1,6 +1,6 @@
{
"name": "@rakh/utils",
"version": "2.0.5",
"version": "2.0.8",
"main": "dist/commonjs/index.js",
"module": "dist/es/index.js",
"jsnext:main": "dist/es/index.js",
@ -11,24 +11,26 @@
"test": "jest --coverage && npm run compile",
"compile:es": "tsc --declaration true --declarationMap true --module esnext --outDir './dist/es'",
"compile:commonjs": "tsc --declaration true --declarationMap true --module commonjs --outDir './dist/commonjs'",
"compile": "npm run clean && npm run compile:es && npm run compile:commonjs"
"compile": "npm run clean && npm run compile:es && npm run compile:commonjs && npm run document",
"document": "./node_modules/.bin/typedoc --plugin typedoc-plugin-markdown --out docs ./ts-src"
},
"author": "Martin Donnelly",
"license": "ISC",
"dependencies": {
"save": "^2.4.0",
"typedoc": "^0.20.36"
"save": "^2.4.0"
},
"devDependencies": {
"@types/jest": "^26.0.23",
"@types/jest": "^29.0.3",
"@types/sinon": "^10.0.0",
"grunt": "^1.3.0",
"jest": "^26.6.3",
"marked": "^2.0.3",
"jest": "^29.0.3",
"marked": "^4.1.0",
"prettier": "^2.3.0",
"sinon": "^10.0.0",
"ts-jest": "^26.5.6",
"ts-node": "^9.1.1",
"sinon": "^14.0.0",
"ts-jest": "^29.0.2",
"ts-node": "^10.9.1",
"typedoc": "^0.23.17",
"typedoc-plugin-markdown": "^3.13.6",
"typescript": "^4.2.4",
"vik": "^0.4.0"
},

3
readme.MD Normal file → Executable file
View File

@ -1,4 +1,5 @@
# Rakh/utils
![Build status](https://travis-ci.org/martind2000/utils.svg?branch=main)
A library of functions that I've been using over the years.
Documentation [here](./docs/modules.md)

0
rollup.config.js Normal file → Executable file
View File

0
scripts/create-index.js Normal file → Executable file
View File

0
scripts/def.json Normal file → Executable file
View File

0
scripts/generate.ts Normal file → Executable file
View File

0
scripts/readme.js Normal file → Executable file
View File

0
scripts/tsconfig.json Normal file → Executable file
View File

0
src/LocalStorage.js Normal file → Executable file
View File

0
src/general/array.js Normal file → Executable file
View File

0
src/general/browser.js Normal file → Executable file
View File

0
src/general/class_reference.js Normal file → Executable file
View File

0
src/general/cookie.js Normal file → Executable file
View File

0
src/general/date.js Normal file → Executable file
View File

0
src/general/index.js Normal file → Executable file
View File

0
src/general/math.js Normal file → Executable file
View File

0
src/general/mobile.js Normal file → Executable file
View File

0
src/general/namespace.js Normal file → Executable file
View File

0
src/general/number.js Normal file → Executable file
View File

0
src/general/object.js Normal file → Executable file
View File

0
src/general/root.js Normal file → Executable file
View File

0
src/general/stack_trace.js Normal file → Executable file
View File

0
src/index.js Normal file → Executable file
View File

0
src/logging/formatters/index.js Normal file → Executable file
View File

0
src/logging/formatters/simple_formatter.js Normal file → Executable file
View File

0
src/logging/handlers/consola_handler.js Normal file → Executable file
View File

0
src/logging/handlers/index.js Normal file → Executable file
View File

0
src/logging/handlers/loggy_handler.js Normal file → Executable file
View File

0
src/logging/handlers/stream_handler.js Normal file → Executable file
View File

0
src/logging/index.js Normal file → Executable file
View File

0
src/logging/logging.js Normal file → Executable file
View File

0
src/utils.js Normal file → Executable file
View File

0
ts-src/LocalStorage.ts Normal file → Executable file
View File

0
ts-src/arrayFromObj.test.ts Normal file → Executable file
View File

0
ts-src/arrayFromObj.ts Normal file → Executable file
View File

0
ts-src/debounce.test.ts Normal file → Executable file
View File

0
ts-src/debounce.ts Normal file → Executable file
View File

0
ts-src/distance.test.ts Normal file → Executable file
View File

0
ts-src/distance.ts Normal file → Executable file
View File

0
ts-src/extractFromObj.test.ts Normal file → Executable file
View File

0
ts-src/extractFromObj.ts Normal file → Executable file
View File

0
ts-src/get.test.ts Normal file → Executable file
View File

0
ts-src/get.ts Normal file → Executable file
View File

0
ts-src/getDays.test.ts Normal file → Executable file
View File

0
ts-src/getDays.ts Normal file → Executable file
View File

0
ts-src/hasOwn.test.ts Normal file → Executable file
View File

0
ts-src/hasOwn.ts Normal file → Executable file
View File

0
ts-src/hourFloor.test.ts Normal file → Executable file
View File

0
ts-src/hourFloor.ts Normal file → Executable file
View File

2
ts-src/index.ts Normal file → Executable file
View File

@ -8,9 +8,11 @@ export * from './getDays';
export * from './hasOwn';
export * from './hourFloor';
export * from './isEmpty';
export * from './kebabCase';
export * from './latLong';
export * from './limitedArray';
export * from './maybePluralize';
export * from './minuteFloor';
export * from './once';
export * from './partOfDay';
export * from './throttle';

0
ts-src/isEmpty.test.ts Normal file → Executable file
View File

0
ts-src/isEmpty.ts Normal file → Executable file
View File

26
ts-src/kebabCase.test.ts Normal file
View File

@ -0,0 +1,26 @@
import { kebabCase} from './kebabCase';
test('Null instead of string', function() {
expect(kebabCase(null)).toBe('')
});
test('Empty string', function() {
expect(kebabCase('')).toBe('')
});
test('String with spaces', function() {
expect(kebabCase('test string')).toBe('test-string')
});
test('String with Capitals', function() {
expect(kebabCase('testString')).toBe('test-string')
});
test('String with underscores', function() {
expect(kebabCase('test_string')).toBe('test-string')
});
test('String with spaces, underscores and capitals', function() {
expect(kebabCase('this is a_complexTest_String')).toBe('this-is-a-complex-test-string')
});

23
ts-src/kebabCase.ts Normal file
View File

@ -0,0 +1,23 @@
/**
* Turn a string into a kebab-case string
* @param inval
*
* @example
* U.kebabCase('test string') // => 'test-string'
* U.kebabCase('testString') // => 'test-string'
* U.kebabCase('test_string') // => 'test-string'
*/
export function kebabCase(inval: string | null): string {
const tempString = (inval) ? inval : '';
const workArray = tempString.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g);
let workString: string = '';
if (workArray && workArray.length > 0) {
workString = workArray!.join('-').toLowerCase();
}
return workString;
}

0
ts-src/latLong.ts Normal file → Executable file
View File

0
ts-src/limitedArray.test.ts Normal file → Executable file
View File

4
ts-src/limitedArray.ts Normal file → Executable file
View File

@ -1,3 +1,7 @@
/**
* A finite array, which is can be limited in size, items added to the end push items off the top.
*/
export class limitedArray {
_array: any[];
_limit: number;

0
ts-src/maybePluralize.test.ts Normal file → Executable file
View File

0
ts-src/maybePluralize.ts Normal file → Executable file
View File

14
ts-src/minuteFloor.test.ts Executable file
View File

@ -0,0 +1,14 @@
import {minuteFloor} from './minuteFloor';
test('Should return the correct Base32 value', () => {
expect(minuteFloor(1605532173)).toEqual('1fr3bi0');
});
test('Should return something for a null value', () => {
expect(minuteFloor(null)).toBeDefined();
});
test('Should return something for an empty value', () => {
expect(minuteFloor()).toBeDefined();
});

15
ts-src/minuteFloor.ts Executable file
View File

@ -0,0 +1,15 @@
/**
* Get the minute floor as a Base 32 string
* @param timestamp The timestamp as a number
* @returns {string}
*
* @example
* U.minuteFloor(1605532173) // => '1fnp540'
*/
export function minuteFloor (timestamp?: number|null): string {
const _timestamp = (timestamp) ? timestamp : Date.now();
return (Math.floor(_timestamp / 60000) * 60000).toString(32);
}

0
ts-src/once.test.ts Normal file → Executable file
View File

0
ts-src/once.ts Normal file → Executable file
View File

39
ts-src/partOfDay.test.ts Normal file → Executable file
View File

@ -1,9 +1,48 @@
import { partOfDay } from './partOfDay';
test('Should return something if null timestring is null', () => {
expect(partOfDay(null, false)).toBeDefined();
});
test('Should return Early Morning', () => {
expect(partOfDay('06:00', false)).toEqual('Early Morning');
});
test('Should return Morning', () => {
expect(partOfDay('08:00', false)).toEqual('Morning');
});
test('Should return Afternoon', () => {
expect(partOfDay('12:00', false)).toEqual('Afternoon');
});
test('Should return Evening', () => {
expect(partOfDay('20:00', false)).toEqual('Evening');
});
test('Should return Night', () => {
expect(partOfDay('22:00', false)).toEqual('Night');
});
test('Should return Late Night', () => {
expect(partOfDay('00:00', false)).toEqual('Late Night');
});
test('Should return Tonight with active todaybit', () => {
expect(partOfDay('22:00', true)).toEqual('this Night');
});
test('Should return night for missing daybit', () => {
expect(partOfDay('22:00')).toEqual('Night');
});
/* if (hours >= 0 && hours < 4) dayBit = 'Late Night';
else if (hours >= 4 && hours < 7) dayBit = 'Early Morning';
else if (hours >= 7 && hours < 12) dayBit = 'Morning';
else if (hours >= 12 && hours < 17) dayBit = 'Afternoon';
else if (hours < 21) dayBit = 'Evening';
else dayBit = 'Night';*/

2
ts-src/partOfDay.ts Normal file → Executable file
View File

@ -8,7 +8,7 @@
* @example
* U.partOfDay('13:00') // => 'Afternoon'
*/
export function partOfDay(timeString: string, today: boolean = false): string {
export function partOfDay(timeString: string|null, today: boolean = false): string {
if (timeString === undefined || timeString === null) timeString = new Date().getHours().toString();
if (today === undefined) today = false;

0
ts-src/throttle.test.ts Normal file → Executable file
View File

0
ts-src/throttle.ts Normal file → Executable file
View File

0
ts-src/toHour.test.ts Normal file → Executable file
View File

0
ts-src/toHour.ts Normal file → Executable file
View File

0
ts-src/utils.txt Normal file → Executable file
View File

0
tsconfig.json Normal file → Executable file
View File