Skip to content

Commit

Permalink
implemented DateService
Browse files Browse the repository at this point in the history
  • Loading branch information
dkvovik committed Dec 5, 2023
1 parent ddf8044 commit 089dd36
Show file tree
Hide file tree
Showing 8 changed files with 395 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/services/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { DateService } from './src/date'

export { HOURS_IN_DAY, MINUTES_IN_HOUR, SECONDS_IN_MINUTE } from './src/date/constants'

export { getTimeBeforeDeadline, getDoubleXpDeadlineText } from './src/date/utils'
30 changes: 30 additions & 0 deletions packages/services/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions packages/services/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@ulms/services",
"version": "1.0.0",
"description": "",
"keywords": [
"lerna"
],
"homepage": "https://github.com/netology-group/ulms-media-ui#readme",
"bugs": {
"url": "https://github.com/netology-group/ulms-media-ui/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/netology-group/ulms-media-ui.git"
},
"license": "MIT",
"author": "",
"main": "index.js",
"module": "es/index.js",
"files": [
"es",
"src"
],
"scripts": {
"build": "rm -rf ./es/* && BABEL_ENV=es rollup --config ./rollup.config.js index.js"
},
"peerDependencies": {
"dayjs": "1.10.7"
}
}
126 changes: 126 additions & 0 deletions packages/services/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/* eslint-disable */
const cssdupl = require('postcss-discard-duplicates')
const cssnext = require('postcss-cssnext')
const cssurl = require('postcss-url')
const Debug = require('debug')
const env = require('postcss-preset-env')
const svgr = require('@svgr/rollup')
import strip from '@rollup/plugin-strip';
import terser from '@rollup/plugin-terser';
import json from '@rollup/plugin-json';
import { nodeResolve as npm } from '@rollup/plugin-node-resolve';
import { babel } from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import postcss from 'rollup-plugin-postcss';
import cssnano from 'cssnano';

const { name, peerDependencies } = require('./package.json')
const { postcssLoader } = require('./rollup/loaders')
const babelrc = require('./.babelrc.json')

const babel_rc = babelrc.env[process.env.BABEL_ENV || 'es']

// const warn = console.warn
console.warn = (...argv) => process.env.LOG_WARN && Debug(`${name}:console.warn`)(...argv)
// monkeypatch warn method to disable annoying postcss warning

// const globalDebug = Debug(`${name}:rollup.config.js`)

const shouldMinifyCss = options => process.env.NODE_ENV === 'production' ? cssnano(options) : []

const shouldUglify = options => process.env.NODE_ENV === 'production' ? [terser(options), strip({
functions: ['console.log', 'assert.*'], // Убираем только console.log, warn и error оставляем!
})] : []

const processAsCssModule = function(){
this.options = {
...this.options, modules: true, namedExports: true,
}
}

const rollupPlugins = [ // order matters
json(),
svgr(),
postcss({
extract: true,
plugins: [
cssurl({ url: 'inline' }),
env(),
cssnext(),
cssdupl()
].concat(shouldMinifyCss()),
loaders: [
{
name: 'postcss',
alwaysProcess: true,
test: /\.css$/,
process (_) {
if(/node_modules\/@foxford\/ui\/.*\.css$/.exec(this.id)){
// do nothing as we going to import file as is
} else if(/node_modules\/.*\.css$/.exec(this.id)){
// do nothing as we going to import file as is
} else {
processAsCssModule.call(this)
// process css as css-modules
}
// :up is crucial to allow transpile local and external .css separately

return postcssLoader.process.call(this, _)
},
},
],
}),
npm({
browser: true,
extensions: ['.js', '.jsx']
}),
commonjs({
include: 'node_modules/**',
namedExports: {
'react-sizeme': ['SizeMe'],
'fabric': ['fabric']
}
}),
babel({
babelrc: false,
presets: babel_rc.presets,
plugins: [
...babelrc.plugins,
...babel_rc.plugins
]
}),
].concat(shouldUglify())

const dist = (entry = 'index.js', frm = './', out = './es') => {
const opts = ({
input: `${frm}/${entry}`,
output: {
file: `${out}/${entry}`,
format: 'es',
sourcemap: true,
},
file: `${out}/${entry}`, // that's important duplicate
external: Object.keys(peerDependencies),
plugins: rollupPlugins,
onwarn: function(warning){
if(!warning.code) return //globalDebug(warning.message)

//const debug = Debug(`${name}:${warning.code}`)

// if(process.env.LOG_DEBUG) debug(warning)

if(warning.code === 'UNKNOWN_OPTION'){
//if(process.env.LOG_DEBUG) debug(warning.message)
// return
} else if(warning.code) {
//return debug(warning.message)
}

//globalDebug(warning.message)
}
})

return opts
}

module.exports = dist()
5 changes: 5 additions & 0 deletions packages/services/src/date/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const HOURS_IN_DAY = 24

export const MINUTES_IN_HOUR = 60

export const SECONDS_IN_MINUTE = 60
60 changes: 60 additions & 0 deletions packages/services/src/date/dayjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import dj from 'dayjs'
import dayjs_en from 'dayjs/locale/en'
import dayjs_ru from 'dayjs/locale/ru'
import customParseFormat from 'dayjs/plugin/customParseFormat'
import isBetween from 'dayjs/plugin/isBetween'
import LocalizedFormat from 'dayjs/plugin/localizedFormat'
import relativeTime from 'dayjs/plugin/relativeTime'
import timezone from 'dayjs/plugin/timezone'
import utc from 'dayjs/plugin/utc'
import weekday from 'dayjs/plugin/weekday'

const LOCALES = {
en: dayjs_en,
ru: dayjs_ru,
}

// Create instance of dayjs with settings https://github.com/iamkun/dayjs/issues/1227
export class DayjsAdapter {
static LOCALES = LOCALES

constructor () {
this.dayjs = dj
this.extend()
}

extend () {
this.dayjs.extend(utc)
this.dayjs.extend(timezone)
this.dayjs.extend(customParseFormat)
this.dayjs.extend(weekday)
this.dayjs.extend(LocalizedFormat)
this.dayjs.extend(isBetween)
this.dayjs.extend(relativeTime)
}

initLocale (locale) {
const browserLocale = navigator.language.split('-')[0]

if (locale === undefined || locale === null) {
this.dayjs.locale(browserLocale)

return
}
if (!DayjsAdapter.LOCALES[locale]) {
this.dayjs.locale('ru', DayjsAdapter.LOCALES.ru)

return
}
this.dayjs.locale(locale, DayjsAdapter.LOCALES[locale])
}

setDefaultLocale = () => {}

setTimezone = (tz = 'Europe/Moscow') => {
this.timezone = tz
this.dayjs.tz.setDefault(tz)
}

getInstance = () => this.dayjs
}
45 changes: 45 additions & 0 deletions packages/services/src/date/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { DayjsAdapter } from './dayjs'

class DateService {
constructor (adapter) {
this.provider = adapter
}

initLocale (locale = navigator.language.split('-')[0]) {
this.provider.initLocale(locale)
}

setDateTimezone (tz) {
this.provider.setTimezone(tz)
}

getProvider () {
return this.provider.getInstance()
}

_ (...args) {
return this.getProvider()(...args)
}

tz (date, timezone) {
// Порядок вызова нельзя трогать, если надо лучше создать отдельный метод
return this.getProvider()(date).tz(timezone)
}

utc (...args) {
return this.getProvider().utc(...args)
}

unix (arg) {
return this.getProvider().unix(arg)
}
}

const adapter = new DayjsAdapter()

adapter.setTimezone()
const service = new DateService(adapter)

service.setDateTimezone(new Intl.DateTimeFormat()?.resolvedOptions?.()?.timeZone)

export { service as DateService }
Loading

0 comments on commit 089dd36

Please sign in to comment.