first commit
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
export class ApiError extends Error {
|
||||
constructor(message) {
|
||||
super(message)
|
||||
this.name = 'ApiError'
|
||||
}
|
||||
}
|
||||
|
||||
export function normalizeApiError(error_) {
|
||||
if (!error_?.response && !error_?.request) {
|
||||
return error_
|
||||
}
|
||||
|
||||
const error = error_.response
|
||||
? new ApiError(`Response error: ${error_?.response?.statusText}`)
|
||||
: new ApiError(`Network error: ${error_?.message}`)
|
||||
|
||||
error.statusCode =
|
||||
!error_.response && error_.code === 'ECONNABORTED'
|
||||
? 408
|
||||
: !error_.response && !error.isTimeoutError
|
||||
? 599
|
||||
: error_?.response?.status || 500
|
||||
|
||||
error.errorType = 'api'
|
||||
error.url = (error_?.config?.baseURL || '') + (error_?.config?.url || '')
|
||||
error.headers = error_?.config?.headers
|
||||
error.data = error_?.config?.data
|
||||
error.timeout = error_?.config?.timeout
|
||||
|
||||
if (error.url) {
|
||||
try {
|
||||
const parsedUrl = new URL(error.url)
|
||||
error.hostname = parsedUrl.hostname
|
||||
error.pathname = parsedUrl.pathname
|
||||
error.search = parsedUrl.search
|
||||
} catch {}
|
||||
}
|
||||
|
||||
return error
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
export const cleanFilters = (filters) =>
|
||||
Object.fromEntries(
|
||||
Object.entries(filters).filter(
|
||||
([, value]) => value !== '' && value !== null && value !== undefined
|
||||
)
|
||||
)
|
||||
|
||||
export default cleanFilters
|
||||
@@ -0,0 +1,11 @@
|
||||
const persianNumbers = [/۰/g, /۱/g, /۲/g, /۳/g, /۴/g, /۵/g, /۶/g, /۷/g, /۸/g, /۹/g]
|
||||
const arabicNumbers = [/٠/g, /١/g, /٢/g, /٣/g, /٤/g, /٥/g, /٦/g, /٧/g, /٨/g, /٩/g]
|
||||
|
||||
export const convertToEnNumber = (str) => {
|
||||
if (typeof str !== 'string') return ''
|
||||
let result = str
|
||||
for (let i = 0; i < 10; i += 1) {
|
||||
result = result.replace(persianNumbers[i], i).replace(arabicNumbers[i], i)
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import isArray from 'lodash/isArray'
|
||||
import isObject from 'lodash/isObject'
|
||||
import camelCase from 'lodash/camelCase'
|
||||
import snakeCase from 'lodash/snakeCase'
|
||||
import transform from 'lodash/transform'
|
||||
|
||||
export function camelize(obj) {
|
||||
return transform(obj, (acc, value, key, target) => {
|
||||
const camelKey = isArray(target) ? key : camelCase(key)
|
||||
acc[camelKey] = isObject(value) ? camelize(value) : value
|
||||
})
|
||||
}
|
||||
|
||||
export function snakize(obj) {
|
||||
return transform(obj, (acc, value, key, target) => {
|
||||
const snakeKey = isArray(target) ? key : snakeCase(key)
|
||||
acc[snakeKey] = isObject(value) ? snakize(value) : value
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,395 @@
|
||||
// Utility helper functions.
|
||||
|
||||
function div(a, b) {
|
||||
return Math.trunc(a / b)
|
||||
}
|
||||
|
||||
function mod(a, b) {
|
||||
return a - Math.trunc(a / b) * b
|
||||
}
|
||||
|
||||
/*
|
||||
Jalaali years starting the 33-year rule.
|
||||
*/
|
||||
const breaks = [
|
||||
-61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210, 1635, 2060, 2097, 2192, 2262, 2324, 2394,
|
||||
2456, 3178,
|
||||
]
|
||||
|
||||
/*
|
||||
This function determines if the Jalaali (Persian) year is
|
||||
leap (366-day long) or is the common year (365 days)
|
||||
|
||||
@param jy Jalaali calendar year (-61 to 3177)
|
||||
@returns number of years since the last leap year (0 to 4)
|
||||
*/
|
||||
function jalCalLeap(jy) {
|
||||
const bl = breaks.length
|
||||
let jp = breaks[0]
|
||||
let jm
|
||||
let jump
|
||||
let leap
|
||||
let n
|
||||
let i
|
||||
|
||||
if (jy < jp || jy >= breaks[bl - 1]) {
|
||||
throw new Error(`Invalid Jalaali year ${jy}`)
|
||||
}
|
||||
|
||||
for (i = 1; i < bl; i += 1) {
|
||||
jm = breaks[i]
|
||||
jump = jm - jp
|
||||
if (jy < jm) {
|
||||
break
|
||||
}
|
||||
jp = jm
|
||||
}
|
||||
n = jy - jp
|
||||
|
||||
if (jump - n < 6) {
|
||||
n = n - jump + div(jump + 4, 33) * 33
|
||||
}
|
||||
leap = mod(mod(n + 1, 33) - 1, 4)
|
||||
if (leap === -1) {
|
||||
leap = 4
|
||||
}
|
||||
|
||||
return leap
|
||||
}
|
||||
|
||||
/*
|
||||
Is this a leap year or not?
|
||||
*/
|
||||
function isLeapJalaaliYear(jy) {
|
||||
return jalCalLeap(jy) === 0
|
||||
}
|
||||
|
||||
/*
|
||||
Number of days in a given month in a Jalaali year.
|
||||
*/
|
||||
export function jalaaliMonthLength(jy, jm) {
|
||||
if (jm <= 6) return 31
|
||||
if (jm <= 11) return 30
|
||||
if (isLeapJalaaliYear(jy)) return 30
|
||||
return 29
|
||||
}
|
||||
|
||||
/*
|
||||
This function determines if the Jalaali (Persian) year is
|
||||
leap (366-day long) or is the common year (365 days), and
|
||||
finds the day in March (Gregorian calendar) of the first
|
||||
day of the Jalaali year (jy).
|
||||
|
||||
@param jy Jalaali calendar year (-61 to 3177)
|
||||
@param withoutLeap when don't need leap (true or false) default is false
|
||||
@return
|
||||
leap: number of years since the last leap year (0 to 4)
|
||||
gy: Gregorian year of the beginning of Jalaali year
|
||||
march: the March day of Farvardin the 1st (1st day of jy)
|
||||
@see: http://www.astro.uni.torun.pl/~kb/Papers/EMP/PersianC-EMP.htm
|
||||
@see: http://www.fourmilab.ch/documents/calendar/
|
||||
*/
|
||||
function jalCal(jy, withoutLeap) {
|
||||
const bl = breaks.length
|
||||
const gy = jy + 621
|
||||
let leapJ = -14
|
||||
let jp = breaks[0]
|
||||
let jm
|
||||
let jump
|
||||
let leap
|
||||
let n
|
||||
let i
|
||||
|
||||
if (jy < jp || jy >= breaks[bl - 1]) {
|
||||
throw new Error(`Invalid Jalaali year ${jy}`)
|
||||
}
|
||||
|
||||
// Find the limiting years for the Jalaali year jy.
|
||||
for (i = 1; i < bl; i += 1) {
|
||||
jm = breaks[i]
|
||||
jump = jm - jp
|
||||
if (jy < jm) {
|
||||
break
|
||||
}
|
||||
leapJ = leapJ + div(jump, 33) * 8 + div(mod(jump, 33), 4)
|
||||
jp = jm
|
||||
}
|
||||
n = jy - jp
|
||||
|
||||
// Find the number of leap years from AD 621 to the beginning
|
||||
// of the current Jalaali year in the Persian calendar.
|
||||
leapJ = leapJ + div(n, 33) * 8 + div(mod(n, 33) + 3, 4)
|
||||
if (mod(jump, 33) === 4 && jump - n === 4) {
|
||||
leapJ += 1
|
||||
}
|
||||
|
||||
// And the same in the Gregorian calendar (until the year gy).
|
||||
const leapG = div(gy, 4) - div((div(gy, 100) + 1) * 3, 4) - 150
|
||||
|
||||
// Determine the Gregorian date of Farvardin the 1st.
|
||||
const march = 20 + leapJ - leapG
|
||||
|
||||
// Find how many years have passed since the last leap year.
|
||||
if (!withoutLeap) {
|
||||
if (jump - n < 6) {
|
||||
n = n - jump + div(jump + 4, 33) * 33
|
||||
}
|
||||
leap = mod(mod(n + 1, 33) - 1, 4)
|
||||
if (leap === -1) {
|
||||
leap = 4
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
leap,
|
||||
gy,
|
||||
march,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Calculates the Julian Day number from Gregorian or Julian
|
||||
calendar dates. This integer number corresponds to the noon of
|
||||
the date (i.e. 12 hours of Universal Time).
|
||||
The procedure was tested to be good since 1 March, -100100 (of both
|
||||
calendars) up to a few million years into the future.
|
||||
|
||||
@param gy Calendar year (years BC numbered 0, -1, -2, ...)
|
||||
@param gm Calendar month (1 to 12)
|
||||
@param gd Calendar day of the month (1 to 28/29/30/31)
|
||||
@return Julian Day number
|
||||
*/
|
||||
function g2d(gy, gm, gd) {
|
||||
let d =
|
||||
div((gy + div(gm - 8, 6) + 100_100) * 1461, 4) +
|
||||
div(153 * mod(gm + 9, 12) + 2, 5) +
|
||||
gd -
|
||||
34_840_408
|
||||
d = d - div(div(gy + 100_100 + div(gm - 8, 6), 100) * 3, 4) + 752
|
||||
return d
|
||||
}
|
||||
|
||||
/*
|
||||
Converts a date of the Jalaali calendar to the Julian Day number.
|
||||
|
||||
@param jy Jalaali year (1 to 3100)
|
||||
@param jm Jalaali month (1 to 12)
|
||||
@param jd Jalaali day (1 to 29/31)
|
||||
@return Julian Day number
|
||||
*/
|
||||
function j2d(jy, jm, jd) {
|
||||
const r = jalCal(jy, true)
|
||||
return g2d(r.gy, 3, r.march) + (jm - 1) * 31 - div(jm, 7) * (jm - 7) + jd - 1
|
||||
}
|
||||
|
||||
/*
|
||||
Calculates Gregorian and Julian calendar dates from the Julian Day number
|
||||
(jdn) for the period since jdn=-34839655 (i.e. the year -100100 of both
|
||||
calendars) to some millions years ahead of the present.
|
||||
|
||||
@param jdn Julian Day number
|
||||
@return
|
||||
gy: Calendar year (years BC numbered 0, -1, -2, ...)
|
||||
gm: Calendar month (1 to 12)
|
||||
gd: Calendar day of the month M (1 to 28/29/30/31)
|
||||
*/
|
||||
function d2g(jdn) {
|
||||
let j = 4 * jdn + 139_361_631
|
||||
j = j + div(div(4 * jdn + 183_187_720, 146_097) * 3, 4) * 4 - 3908
|
||||
const i = div(mod(j, 1461), 4) * 5 + 308
|
||||
const gd = div(mod(i, 153), 5) + 1
|
||||
const gm = mod(div(i, 153), 12) + 1
|
||||
const gy = div(j, 1461) - 100_100 + div(8 - gm, 6)
|
||||
return {
|
||||
gy,
|
||||
gm,
|
||||
gd,
|
||||
}
|
||||
}
|
||||
/*
|
||||
Converts the Julian Day number to a date in the Jalaali calendar.
|
||||
|
||||
@param jdn Julian Day number
|
||||
@return
|
||||
jy: Jalaali year (1 to 3100)
|
||||
jm: Jalaali month (1 to 12)
|
||||
jd: Jalaali day (1 to 29/31)
|
||||
*/
|
||||
function d2j(jdn) {
|
||||
const { gy } = d2g(jdn) // Calculate Gregorian year (gy).
|
||||
let jy = gy - 621
|
||||
let jd
|
||||
let jm
|
||||
let k
|
||||
const r = jalCal(jy, false)
|
||||
const jdn1f = g2d(gy, 3, r.march)
|
||||
|
||||
// Find number of days that passed since 1 Farvardin.
|
||||
k = jdn - jdn1f
|
||||
if (k >= 0) {
|
||||
if (k <= 185) {
|
||||
// The first 6 months.
|
||||
jm = 1 + div(k, 31)
|
||||
jd = mod(k, 31) + 1
|
||||
return {
|
||||
jy,
|
||||
jm,
|
||||
jd,
|
||||
}
|
||||
}
|
||||
|
||||
// The remaining months.
|
||||
k -= 186
|
||||
} else {
|
||||
// Previous Jalaali year.
|
||||
jy -= 1
|
||||
k += 179
|
||||
if (r.leap === 1) {
|
||||
k += 1
|
||||
}
|
||||
}
|
||||
jm = 7 + div(k, 30)
|
||||
jd = mod(k, 30) + 1
|
||||
return {
|
||||
jy,
|
||||
jm,
|
||||
jd,
|
||||
}
|
||||
}
|
||||
/*
|
||||
Converts a Gregorian date to Jalaali.
|
||||
*/
|
||||
export function toJalaali(gy, gm, gd) {
|
||||
let cloneGd = gd
|
||||
let cloneGm = gm
|
||||
let cloneGy = gy
|
||||
if (Object.prototype.toString.call(gy) === '[object Date]') {
|
||||
cloneGd = gy.getDate()
|
||||
cloneGm = gy.getMonth() + 1
|
||||
cloneGy = gy.getFullYear()
|
||||
}
|
||||
return d2j(g2d(cloneGy, cloneGm, cloneGd))
|
||||
}
|
||||
|
||||
/*
|
||||
Converts a Jalaali date to Gregorian.
|
||||
*/
|
||||
export function toGregorian(jy, jm, jd) {
|
||||
return d2g(j2d(jy, jm, jd))
|
||||
}
|
||||
|
||||
export function convertToJalali(gregorianDate, format = 'jYYYY/jMM/jDD') {
|
||||
const months = 'فروردین_اردیبهشت_خرداد_تیر_مرداد_شهریور_مهر_آبان_آذر_دی_بهمن_اسفند'.split('_')
|
||||
const weekdays = 'یک\u200Cشنبه_دوشنبه_سه\u200Cشنبه_چهارشنبه_پنج\u200Cشنبه_جمعه_شنبه'.split('_')
|
||||
|
||||
const date = new Date(gregorianDate)
|
||||
|
||||
const weekday = date.getDay()
|
||||
|
||||
const { jy: year, jm: month, jd: day } = toJalaali(date)
|
||||
|
||||
const formatMap = {
|
||||
jYYYY: year.toString(),
|
||||
jYY: year.toString().slice(-2),
|
||||
jMMMM: months[month - 1],
|
||||
jMMM: months[month - 1].slice(0, 3),
|
||||
jMM: month.toString().padStart(2, '0'),
|
||||
jM: month.toString(),
|
||||
jDD: day.toString().padStart(2, '0'),
|
||||
jD: day.toString(),
|
||||
jdddd: weekdays[weekday],
|
||||
jddd: weekdays[weekday].slice(0, 3),
|
||||
}
|
||||
|
||||
return format.replace(
|
||||
/jYYYY|jYY|jMMMM|jMMM|jMM|jM|jDD|jD|jdddd|jddd/g,
|
||||
(match) => formatMap[match]
|
||||
)
|
||||
}
|
||||
|
||||
export function formatJalaaliDate(value, { fallback = '' } = {}) {
|
||||
if (!value) return fallback
|
||||
const date = new Date(value)
|
||||
if (Number.isNaN(date.getTime())) return fallback
|
||||
return convertToJalali(date, 'jYYYY/jMM/jDD')
|
||||
}
|
||||
|
||||
export function formatJalaaliDateTime(value, { fallback = '', separator = ' - ' } = {}) {
|
||||
if (!value) return fallback
|
||||
const date = new Date(value)
|
||||
if (Number.isNaN(date.getTime())) return fallback
|
||||
const datePart = convertToJalali(date, 'jYYYY/jMM/jDD')
|
||||
const hh = String(date.getHours()).padStart(2, '0')
|
||||
const mm = String(date.getMinutes()).padStart(2, '0')
|
||||
return `${datePart}${separator}${hh}:${mm}`
|
||||
}
|
||||
|
||||
export function formatTime(value, { fallback = '' } = {}) {
|
||||
if (!value) return fallback
|
||||
const date = new Date(value)
|
||||
if (Number.isNaN(date.getTime())) return fallback
|
||||
const hh = String(date.getHours()).padStart(2, '0')
|
||||
const mm = String(date.getMinutes()).padStart(2, '0')
|
||||
return `${hh}:${mm}`
|
||||
}
|
||||
|
||||
export function jalaaliStringToIsoDate(jalaliStr) {
|
||||
if (!jalaliStr) return null
|
||||
const [jy, jm, jd] = String(jalaliStr).split('/').map(Number)
|
||||
if (!jy || !jm || !jd) return null
|
||||
try {
|
||||
const { gy, gm, gd } = toGregorian(jy, jm, jd)
|
||||
const month = String(gm).padStart(2, '0')
|
||||
const day = String(gd).padStart(2, '0')
|
||||
return `${gy}-${month}-${day}T00:00:00.000Z`
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function jalaaliStringToIsoDateTime(jalaliDateTimeStr) {
|
||||
if (!jalaliDateTimeStr) return null
|
||||
const [datePart, timePart = '00:00'] = String(jalaliDateTimeStr).split(' ')
|
||||
const [jy, jm, jd] = datePart.split('/').map(Number)
|
||||
if (!jy || !jm || !jd) return null
|
||||
try {
|
||||
const { gy, gm, gd } = toGregorian(jy, jm, jd)
|
||||
const month = String(gm).padStart(2, '0')
|
||||
const day = String(gd).padStart(2, '0')
|
||||
return `${gy}-${month}-${day}T${timePart}:00.000Z`
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function convertToGregorian(jalaaliDate, format = 'YYYY/MM/DD') {
|
||||
const weekdays = 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_')
|
||||
const months =
|
||||
'January_February_March_April_May_June_July_August_September_October_November_December'.split(
|
||||
'_'
|
||||
)
|
||||
|
||||
const date = new Date(jalaaliDate)
|
||||
const jYear = date.getFullYear()
|
||||
const jMonth = date.getMonth()
|
||||
const jDay = date.getDate()
|
||||
|
||||
const { gy: year, gm: month, gd: day } = toGregorian(jYear, jMonth + 1, jDay)
|
||||
|
||||
const gWeekday = new Date([year, month, day]).getDay()
|
||||
|
||||
const formatMap = {
|
||||
YYYY: year.toString(),
|
||||
YY: year.toString().slice(-2),
|
||||
MMMM: months[month - 1],
|
||||
MMM: months[month - 1].slice(0, 3),
|
||||
MM: month.toString().padStart(2, '0'),
|
||||
M: month.toString(),
|
||||
DD: day.toString().padStart(2, '0'),
|
||||
D: day.toString(),
|
||||
dddd: weekdays[gWeekday],
|
||||
ddd: weekdays[gWeekday].slice(0, 3),
|
||||
}
|
||||
|
||||
return format.replace(/YYYY|YY|MMMM|MMM|MM|M|DD|D|dddd|ddd/g, (match) => formatMap[match])
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import { toast } from 'vue3-toastify'
|
||||
|
||||
import { normalizeApiError } from './api-error'
|
||||
|
||||
export function humanizeError(error) {
|
||||
let currentError
|
||||
if (error?.response || error?.request) {
|
||||
currentError = normalizeApiError(error)
|
||||
} else if (error instanceof Error) {
|
||||
currentError = error
|
||||
currentError.errorType = 'logic'
|
||||
} else {
|
||||
currentError = new Error('something unexpected', { cause: error })
|
||||
currentError.errorType = 'unknown'
|
||||
}
|
||||
|
||||
if (!currentError.statusCode) {
|
||||
currentError.statusCode = 500
|
||||
}
|
||||
return currentError
|
||||
}
|
||||
|
||||
export function handleApiError(error_) {
|
||||
const data = error_?.response?.data
|
||||
const status = error_?.response?.status
|
||||
|
||||
if (status === 422 && data?.errors) {
|
||||
Object.values(data.errors).forEach((messages) => {
|
||||
const list = Array.isArray(messages) ? messages : [messages]
|
||||
list.forEach((msg) => toast.error(msg))
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (data?.message) {
|
||||
toast.error(data.message)
|
||||
return
|
||||
}
|
||||
|
||||
toast.error('عملیات با خطا مواجه شد')
|
||||
}
|
||||
|
||||
export function handleUnknownError(options) {
|
||||
const { showToast } = { showToast: true, ...options }
|
||||
if (showToast) {
|
||||
toast.error('مشکلی در انجام عملیات رخ داد. لطفا دوباره امتحان کنید.')
|
||||
}
|
||||
}
|
||||
|
||||
export function handleError(error, options) {
|
||||
try {
|
||||
const { showToast } = { showToast: true, ...options }
|
||||
const currentError = humanizeError(error)
|
||||
if (currentError.errorType === 'logic') {
|
||||
if (showToast) toast.error(currentError.message)
|
||||
return
|
||||
}
|
||||
if (showToast && currentError.errorType === 'api') {
|
||||
handleApiError(error)
|
||||
return
|
||||
}
|
||||
if (showToast) handleUnknownError(options)
|
||||
} catch {
|
||||
handleUnknownError(options)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import loginPattern from '@/assets/images/auth/login-pattern.jpg'
|
||||
import loginPatternMobile from '@/assets/images/auth/login-pattern-mobile.png'
|
||||
import loginPatternWithLogo from '@/assets/images/auth/login-pattern-logo.jpg'
|
||||
import registrationCompleteFlower from '@/assets/images/auth/flower.png'
|
||||
import logoWhite from '@/assets/images/logo-white.png'
|
||||
import logoPinkishRed from '@/assets/images/logo-pinkish-red.png'
|
||||
import authLogo from '@/assets/images/auth/auth-logo.png'
|
||||
import VideoCover from '@/assets/images/cover.png'
|
||||
|
||||
export const gallery = {
|
||||
loginPattern,
|
||||
loginPatternMobile,
|
||||
authLogo,
|
||||
loginPatternWithLogo,
|
||||
logoWhite,
|
||||
logoPinkishRed,
|
||||
VideoCover,
|
||||
registrationCompleteFlower,
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export const globalComponents = {}
|
||||
@@ -0,0 +1,67 @@
|
||||
const isUndefined = (value) => value === undefined
|
||||
const isNull = (value) => value === null
|
||||
const isBoolean = (value) => typeof value === 'boolean'
|
||||
const isObject = (value) => value === Object(value)
|
||||
const isArray = (value) => Array.isArray(value)
|
||||
const isDate = (value) => value instanceof Date
|
||||
|
||||
const isBlob = (value) =>
|
||||
value &&
|
||||
typeof value.size === 'number' &&
|
||||
typeof value.type === 'string' &&
|
||||
typeof value.slice === 'function'
|
||||
|
||||
const isFile = (value) =>
|
||||
isBlob(value) &&
|
||||
typeof value.name === 'string' &&
|
||||
(typeof value.lastModifiedDate === 'object' || typeof value.lastModified === 'number')
|
||||
|
||||
export const objectToFormData = (obj, cfg = {}, fd, pre) => {
|
||||
const config = {
|
||||
indices: isUndefined(cfg.indices) ? true : cfg.indices,
|
||||
nullsAsUndefineds: isUndefined(cfg.nullsAsUndefineds) ? false : cfg.nullsAsUndefineds,
|
||||
booleansAsIntegers: isUndefined(cfg.booleansAsIntegers) ? false : cfg.booleansAsIntegers,
|
||||
allowEmptyArrays: isUndefined(cfg.allowEmptyArrays) ? true : cfg.allowEmptyArrays,
|
||||
}
|
||||
|
||||
const formData = fd || new FormData()
|
||||
|
||||
if (isUndefined(obj)) {
|
||||
return formData
|
||||
}
|
||||
|
||||
if (isNull(obj)) {
|
||||
if (!config.nullsAsUndefineds) {
|
||||
formData.append(pre, '')
|
||||
}
|
||||
} else if (isBoolean(obj)) {
|
||||
formData.append(pre, config.booleansAsIntegers ? (obj ? 1 : 0) : obj)
|
||||
} else if (isArray(obj)) {
|
||||
if (obj.length > 0) {
|
||||
obj.forEach((value, index) => {
|
||||
const key = `${pre}[${config.indices ? index : ''}]`
|
||||
objectToFormData(value, config, formData, key)
|
||||
})
|
||||
} else if (config.allowEmptyArrays) {
|
||||
formData.append(pre, [])
|
||||
}
|
||||
} else if (isDate(obj)) {
|
||||
formData.append(pre, obj.toISOString())
|
||||
} else if (isObject(obj) && !isFile(obj) && !isBlob(obj)) {
|
||||
Object.keys(obj).forEach((rawProp) => {
|
||||
const value = obj[rawProp]
|
||||
let prop = rawProp
|
||||
if (isArray(value)) {
|
||||
while (prop.length > 2 && prop.lastIndexOf('[]') === prop.length - 2) {
|
||||
prop = prop.slice(0, Math.max(0, prop.length - 2))
|
||||
}
|
||||
}
|
||||
const key = pre ? `${pre}[${prop}]` : prop
|
||||
objectToFormData(value, config, formData, key)
|
||||
})
|
||||
} else {
|
||||
formData.append(pre, obj)
|
||||
}
|
||||
|
||||
return formData
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { setLocale } from 'yup'
|
||||
|
||||
import { fields } from '@/constants/fields'
|
||||
|
||||
const label = (path) => fields[path] || path
|
||||
|
||||
setLocale({
|
||||
mixed: {
|
||||
required: ({ path }) => `فیلد ${label(path)} ضروری است`,
|
||||
},
|
||||
string: {
|
||||
min: ({ path, min }) => `فیلد ${label(path)} باید حداقل ${min} کاراکتر باشد`,
|
||||
max: ({ path, max }) => `فیلد ${label(path)} باید حداکثر ${max} کاراکتر باشد`,
|
||||
email: ({ path }) => `فیلد ${label(path)} باید یک ایمیل معتبر باشد`,
|
||||
url: ({ path }) => `فیلد ${label(path)} باید یک URL معتبر باشد`,
|
||||
length: ({ path, length }) => `فیلد ${label(path)} باید دقیقا ${length} کاراکتر باشد`,
|
||||
},
|
||||
array: {
|
||||
min: ({ path, min }) => `فیلد ${label(path)} باید حداقل ${min} آیتم داشته باشد`,
|
||||
max: ({ path, max }) => `فیلد ${label(path)} باید حداکثر ${max} آیتم داشته باشد`,
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user