@triko-app/utils
v1.0.7
Published
Util functions for triko project
Downloads
6
Readme
@triko-app/utils
Provides Functions to parse, to format, etc.
Functions
- classNames
- explodeNames
- formatDistance
- getDistance
- getElapsedTime
- getPassedTime
- getTrikoAttrs
- isEmpty
- isGreaterThen
- isIn
- isValidEmail
- noIsEmpty
- upcfs
classNames
Allows to combine classes depending on conditions and override classes depending on conditions
type styleType = ViewStyle | ImageStyle | TextStyle
type classNames = (
classesToApply: {[key: string]: boolean},
classes: styleType,
override?: styleType
) => styleType
Args
| name | required | description | default |
| :-- | :--: | :-- | :-- |
| classesToApply | Yes
| Class names to conditionally add | |
| classes | Yes
| Object containing the stylesheet styles | |
| override | No
| Stylesheet with styles which will override any other style | |
Usage
import {classNames} from '@triko-app/utils'
const styles = () => ({
caption: {
// ... some styles for caption
},
selectedText: {
// Some styles to apply when prop selected === true
},
});
const MyComponent: React.FC<{selected: boolean}> = ({selected}) => {
const [classes] = useStyles(styles)
return (
<Text
variant="caption"
style={classNames(
{
caption: true,
// Only when this condition is true the 'selectedText' styles will be applied
selectedText: selected,
},
classes,
)}>
Some cool text
</Text>
)
}
explodeNames
Explodes a full name into first name, last name
type explodeNames = (fullName: string) => string[]
Args
| name | required | description | default |
| :-- | :--: | :-- | :-- |
| fullName | Yes
| The name to be explode, the code will try to separate parts by space char | |
Usage
import {explodeName} from '@triko-app/utils'
const [firstName, lastName] = explodeNames('John Doe')
console.log(firstName) // output -> John
console.log(lastName) // output -> Doe
formatDistance
Format a given distance
type formatDistance = (distance?: number) => string
Args
| name | required | description | default |
| :-- | :--: | :-- | :-- |
| distance | No
| Distance in meters to be formatted to Km | 0
|
Usage
import {formatDistance} from '@triko-app/utils'
const input: number = 10000; // meters
const formatted: string = formatDistance(input)
console.log(formatted) // Output -> 10km
getDistance
Calc the distance between two given lat/lng points
type LatLngType = {
lat: string | number
lng: string | number
}
type getDistance = (from: LatLngType, to: LatLngType) => number | boolean
Args
| name | required | description | default |
| :-- | :--: | :-- | :-- |
| from | Yes
| Starting point | |
| to | Yes
| Destination point | |
Usage
import {getDistance, formatDistance} from '@triko-app/utils'
const pointFrom = {lat: 121231123, lng: 123123123}
const pointTo = {lat: 18191, lng: 8222929}
const distance = getDistance(pointFrom, pointTo)
console.log(formatDistance(distance)) // Output -> Distance converted to Km, eg. 10km
getElapsedTime
Returns elapsed time for two given dates (String format)
type PiecesType = {
days?: number
hours?: number
minutes?: number
seconds?: number
}
type getElapsedTime = (start: string, end: string, pieces?: boolean, format?: string) => string | PiecesType
Args
| name | required | description | default |
| :-- | :--: | :-- | :-- |
| start | Yes
| Starting date in string format | YYYY-MM-DD HH:mm:ss
|
| end | Yes
| Ending date in string format | YYYY-MM-DD HH:mm:ss
|
| pieces | No
| If you want to receive an object with the pieces | false
|
| format | No
| The format for input and output dates | 'YYYY-MM-DD HH:mm:ss'
|
Usage
import {getElapsedTime} from '@triko-app/utils'
const {days, hours, minutes, seconds} = getElapsed('01-01-2021 14:00:00', '01-01-2021 15:35:33', true)
const elapsed = getElapsed('01-01-2021 14:00:00', '01-01-2021 15:35:33', false)
console.log(days, hours, minutes, seconds) // Output -> 0, 1, 35, 33
console.log(elapsed) // 1h 35m 33s
This function it's used to check time differences on service execution
getPassedTime
Returns de difference between the given object and the current date
type getPassedTime = (dateObj: moment.Moment): string
Args
| name | required | description | default |
| :-- | :--: | :-- | :-- |
| dateObj | Yes
| the date to be compared with the current date | moment()
|
Usage
import moment from 'moment'
// Current date 01-12-2021 18:00:00
const passed: moment.Moment = moment('01-10-2021 18:00:00', 'YYYY-MM-DD HH:mm:ss')
console.log(passed) // Output -> 2 days ago
This function it's used to check elapsed time on comments, posts...
getTrikoAttrs
Extracts the triko attributes from it's user.
type TrikoType = {
user: {
attrs: string | { [key: string]: any }
id: number | string
}
}
type getTrikoAttrs = (triko: TrikoType) => {[key: string]: any}
Args
| name | required | description | default |
| :-- | :--: | :-- | :-- |
| triko | Yes
| The triko object | |
Usage
import {getTrikoAttrs} from '@triko-app/utils'
const attrs = getTrikoAttrs(triko)
This function ensures no errors will occur when parsing the attrs object wich cannot be defined or wrong formatted
isEmpty
Checks if a given value it's empty
type isEmpty = (value: string | number | boolean | undefined | null) => boolean
Args
| name | required | description | default |
| :-- | :--: | :-- | :-- |
| value | yes
| The value to be compared | |
Usage
import {isEmpty} from '@triko-app/utils'
const foo = 1
if (!isEmpty(foo)) {
// Some logic if the variable has some value
} else {
// Some login if the value is empty
}
This function was implemented due to javascript engine confusion with empty values (it's not the same null to undefined and 0, sometimes you want 0 to count as a value)
isGreaterThen
Checks two dates (in string format) it's greather then the other.
type isGreaterThen = (timeFrom: string, timeTo: string, format?: string) => boolean
Args
| name | required | description | default |
| :-- | :--: | :-- | :-- |
| timeFrom | Yes
| The starting date to validate | |
| timeTo | Yes
| The end date to validate | |
| format | No
| The format to parse the dates to object | HH:mm:ss
|
Usage
import {isGreaterThen} from '@triko-app/utils'
if (isGreaterThen('14:00:00', '13:30:00')) {
// Logic if true
} else {
// Login if false
}
isIn
Validates if a given value it's in a collection of values
type ValueType = boolean | number | string | null | undefined
type isIn = (value: ValueType, stack: ValueType[]) => boolean
Args
| name | required | description | default |
| :-- | :--: | :-- | :-- |
| value | Yes
| The value to search | |
| stack | Yes
| Array of values to search in | |
Usage
import {isIn} from '@triko-app/utils'
if (isIn(1, [2,4,5, 5, 1])) {
// Logic if true
} else {
// Logic if false
}
isValidEmail
Checks if a given value it's a valid email
type isValidEmail = (email: string) => boolean
Args
| name | required | description | default |
| :-- | :--: | :-- | :-- |
| email | Yes
| String containing the email to validate | |
Usage
import {isValidEmail} from '@triko-app/utils'
if (isValidEmail('[email protected]')) {
// Logic if true
} else {
// Logic if false
}
oneIsEmpty
Checks if one of the given values it's empty
type ValueType = string | number | boolean | undefined
type oneIsEmpty = (values: ValueType) => boolean
Args
| name | required | description | default |
| :-- | :--: | :-- | :-- |
| values | Yes
| Values to loop and check if one is empty | |
Usage
import {oneIsEmpty} from '@triko-app/utils'
const value1 = 'Jako'
const value2 = null
const value3 = 3
const value4 = 0
if (oneIsEmpty([value1, value2, value3, value4])) {
// Logic if at least one value is empty
} else {
// Logic if all the values had content
}
upcfs
Converts the first letter to upper case first
type upcfs = (value: string) => string
Args
| name | required | description | default |
| :-- | :--: | :-- | :-- |
| value | Yes
| String to be formatted | |
Usage
import {upcfs} from '@triko-app/utils'
console.log(upcfs('alejandro')) // Output -> Alejandro