src/lib cleanup
This commit is contained in:
1 parent
6e110e3dd7
commit
729e09d061
7 files changed
+12
-114
No files matched your search
@@ -1,36 +0,0 @@
|
||||
function compose(...fns) {
|
||||
return value => [...fns]
|
||||
.reverse()
|
||||
.reduce((acc, fn) => fn(acc), value)
|
||||
}
|
||||
|
||||
/**
|
||||
* AND operator for list of values
|
||||
*
|
||||
* [true, true, true, ...] -> true
|
||||
* [false, true, true, ...] -> false
|
||||
*
|
||||
* @param list
|
||||
* @return {boolean}
|
||||
*/
|
||||
function all(list) {
|
||||
return list.reduce((t, i) => t && i, true)
|
||||
}
|
||||
|
||||
/**
|
||||
* OR operator for list of values
|
||||
*
|
||||
* [true, true, true, ...] -> true
|
||||
* [false, true, true, ...] -> true
|
||||
* [false, false, false, ...] -> false
|
||||
*
|
||||
* @param list
|
||||
* @return {boolean}
|
||||
*/
|
||||
function any(list) {
|
||||
return list.reduce((t, i) => t || i, false)
|
||||
}
|
||||
|
||||
exports.compose = compose
|
||||
exports.all = all
|
||||
exports.any = any
|
||||
@@ -13,11 +13,3 @@ export const dateFormat = (date: string | Date) => {
|
||||
locale: ru
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Date -> Unix time
|
||||
*
|
||||
* @param date
|
||||
*/
|
||||
export const timestamp = (date: Date) => date.getTime()
|
||||
+9
-2
@@ -5,7 +5,7 @@ export type ResizeOptions = {
|
||||
height: number
|
||||
}
|
||||
|
||||
export function resizeImage(src: string, options: ResizeOptions): string {
|
||||
export function getResizedUrl(src: string, options: ResizeOptions): string {
|
||||
if (!src || src === '') {
|
||||
return null
|
||||
}
|
||||
@@ -17,7 +17,7 @@ export function imageSrcSet(src: string, sizes: ImageSize[]): string {
|
||||
const set = sizes
|
||||
.map(x => ({
|
||||
value: x,
|
||||
href: resizeImage(src, {
|
||||
href: getResizedUrl(src, {
|
||||
width: x,
|
||||
height: x,
|
||||
}),
|
||||
@@ -27,3 +27,10 @@ export function imageSrcSet(src: string, sizes: ImageSize[]): string {
|
||||
.map(x => `${x.href} ${x.value}w`)
|
||||
.join(', ')
|
||||
}
|
||||
|
||||
export function imageSrc(src: string, size: ImageSize): string {
|
||||
return getResizedUrl(src, {
|
||||
width: size,
|
||||
height: size,
|
||||
})
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
function prioritySort(priority, key, list) {
|
||||
priority = priority.reverse()
|
||||
|
||||
let weight = i => {
|
||||
const p = key(i)
|
||||
const index = priority.indexOf(p)
|
||||
return index > -1
|
||||
? index + 1
|
||||
: 0
|
||||
}
|
||||
|
||||
return sort(list, weight)
|
||||
}
|
||||
|
||||
function sort(list, fn, reverse = false) {
|
||||
return list
|
||||
.map(i => [fn(i), i])
|
||||
.sort((a, b) => reverse
|
||||
? a[0] - b[0]
|
||||
: b[0] - a[0])
|
||||
.map(i => i[1])
|
||||
}
|
||||
|
||||
function sortBy(fn, reverse = false) {
|
||||
return (i1, i2) => {
|
||||
const t1 = fn(i1)
|
||||
const t2 = fn(i2)
|
||||
|
||||
return reverse
|
||||
? t1 - t2
|
||||
: t2 - t1
|
||||
}
|
||||
}
|
||||
|
||||
exports.prioritySort = prioritySort
|
||||
exports.sortBy = sortBy
|
||||
exports.sort = sort
|
||||
@@ -1,19 +0,0 @@
|
||||
export async function find(collection, query, {sort, skip, limit}) {
|
||||
const cursor = collection.find(query)
|
||||
|
||||
if (sort) cursor.sort(sort)
|
||||
if (skip) cursor.skip(skip)
|
||||
if (limit) cursor.limit(limit)
|
||||
|
||||
return cursor.toArray()
|
||||
}
|
||||
|
||||
export async function findOne(collection, query) {
|
||||
return collection.findOne(query)
|
||||
}
|
||||
|
||||
export async function total(collection, query) {
|
||||
return collection
|
||||
.find(query)
|
||||
.count()
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
function titleCase(str) {
|
||||
return str.charAt(0).toUpperCase() + str.slice(1)
|
||||
}
|
||||
|
||||
function trim(string) {
|
||||
return string
|
||||
.replace(/^\s+/, '')
|
||||
.replace(/\s+$/, '')
|
||||
}
|
||||
|
||||
exports.titleCase = titleCase
|
||||
exports.trim = trim
|
||||
@@ -0,0 +1,3 @@
|
||||
export function titleCase(str: string) {
|
||||
return str.charAt(0).toUpperCase() + str.slice(1)
|
||||
}
|
||||
Reference in new issue
Block a user