1.0.7 • Published 4 years ago

@triko-app/utils v1.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

@triko-app/utils

Provides Functions to parse, to format, etc.

Functions

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

namerequireddescriptiondefault
classesToApplyYesClass names to conditionally add
classesYesObject containing the stylesheet styles
overrideNoStylesheet 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

namerequireddescriptiondefault
fullNameYesThe 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

namerequireddescriptiondefault
distanceNoDistance in meters to be formatted to Km0

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

namerequireddescriptiondefault
fromYesStarting point
toYesDestination 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

namerequireddescriptiondefault
startYesStarting date in string formatYYYY-MM-DD HH:mm:ss
endYesEnding date in string formatYYYY-MM-DD HH:mm:ss
piecesNoIf you want to receive an object with the piecesfalse
formatNoThe 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

namerequireddescriptiondefault
dateObjYesthe date to be compared with the current datemoment()

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

namerequireddescriptiondefault
trikoYesThe 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

namerequireddescriptiondefault
valueyesThe 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

namerequireddescriptiondefault
timeFromYesThe starting date to validate
timeToYesThe end date to validate
formatNoThe format to parse the dates to objectHH: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

namerequireddescriptiondefault
valueYesThe value to search
stackYesArray 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

namerequireddescriptiondefault
emailYesString containing the email to validate

Usage

import {isValidEmail} from '@triko-app/utils'
if (isValidEmail('alejo.devop@gmail.c')) {
    // 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

namerequireddescriptiondefault
valuesYesValues 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

namerequireddescriptiondefault
valueYesString to be formatted

Usage

import {upcfs} from '@triko-app/utils'
console.log(upcfs('alejandro')) // Output -> Alejandro

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago