Repository URL to install this package:
|
Version:
3.0.12 ▾
|
import { application } from '@skava/state'
import { isArray, isNumber } from 'exotic'
import { MediaCarouselState } from './state'
import { MediaCarouselProps } from './typings'
function initMediaCarouselState() {
return new MediaCarouselState()
}
function toMediaCarouselState(props: MediaCarouselProps) {
if (props.state === undefined) {
const state = initMediaCarouselState()
state.update(props)
return state
} else {
return props.state
}
}
const getDeviceColSpan = (props: MediaCarouselProps) => {
const currentDevice = application.deviceType
return props[currentDevice + 'ColSpan']
}
const getCarouselPanelWidth = (props: MediaCarouselProps) => {
const { list } = props
if (isArray(list) !== undefined) {
const deviceColSpan = getDeviceColSpan(props)
const deviceColSpanAsNumber = getValidNumber(deviceColSpan)
const colSpan = (deviceColSpanAsNumber > list.length ? list.length : deviceColSpanAsNumber)
return 100 / colSpan
} else {
return 100
}
}
// Added temporarily :: Needs to be imported from mono-libraries exotic
function getValidString(str: string) {
if (str) {
const trimmedStr = str.trim()
if (trimmedStr && trimmedStr.length > 0) {
return trimmedStr
}
}
return ''
}
// Added temporarily :: Needs to be imported from mono-libraries exotic
function getValidNumber(num: string | number) {
/**
* @todo
* 10px -> 10
* 10 -> 10
* remove special characters (1 0 px -> 10)
* return as Number data type
*/
if (isNumber(num)) {
return num
}
const validNumberString = getValidString(num)
const formatNumber = validNumberString.replace(/[^0-9-.]+/, '')
console.log('formatNumber', formatNumber)
return +formatNumber
}
// Added temporarily :: Needs to be imported from mono-libraries exotic
function addProtocolsWithURL(url: string) {
const validURL = getValidString(url)
if (validURL && validURL.length > 0) {
const noProtocolURL = validURL.replace(/(^\w+:|^)\/\//, '')
const updatedURL = '//' + noProtocolURL
return updatedURL
}
return validURL
}
export {
getDeviceColSpan,
getCarouselPanelWidth,
toMediaCarouselState,
initMediaCarouselState,
getValidString,
getValidNumber,
addProtocolsWithURL,
}