-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create package "@eventespresso/config" #1302
Changes from all commits
de48483
b7b3806
8f3eb54
7844d05
59cfd8f
d6d23fa
7a6f339
4d707ef
c0c4bcf
f178abe
cc4e39e
486418b
2aef570
81e1183
42932ad
2ee9142
9fdcae0
af09f88
6d73f6d
5b15388
7b333bc
cfc0d99
49162e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module.exports = [ | ||
/* LEVEL 0 */ ['types'], | ||
/* LEVEL -1 */ ['types'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basement! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI... those indexes are just for display purposes... you could have just changed them to go from 1 - 10, or 1 - 11, or whatever There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I figured that. Didn't want to introduce unnecessary change to PR hence |
||
/* LEVEL 0 */ ['config'], | ||
/* LEVEL 1 */ ['constants', 'i18n', 'events', 'ioc', 'toaster', 'utils'], | ||
/* LEVEL 2 */ ['data', 'dates', 'hooks', 'icons', 'plugins', 'slot-fill', 'registry', 'styles', 'storage'], | ||
/* LEVEL 3 */ ['adapters', 'predicates'], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"author": "Event Espresso", | ||
"name": "@eventespresso/config", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/eventespresso/barista.git" | ||
}, | ||
"private": true, | ||
"version": "1.0.0", | ||
"main": "src/index.ts", | ||
"scripts": { | ||
"lint": "eslint ./src/**/*.{ts,tsx} --format=codeframe", | ||
"lint-fix": "eslint ./src/**/*.{js,jsx,ts,tsx} --format=codeframe --fix", | ||
"update-deps": "npx ncu -u" | ||
}, | ||
"sideEffects": false, | ||
"license": "GPL-3.0" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Type } from '.'; | ||
|
||
export function BrandName(config?: Partial<Type.BrandName>): Type.BrandName { | ||
return config ?? 'Event Espresso'; | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,26 @@ | ||||
import type { Type } from '.'; | ||||
|
||||
export const Currency = (config?: Partial<Type.Currency>): Type.Currency => { | ||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||||
const { subunits: exclude, ...defaults } = defaultCurrency; | ||||
const subunits = Subunits(config); | ||||
return { ...defaults, ...config, subunits }; | ||||
}; | ||||
|
||||
function Subunits(config?: Partial<Type.Currency>): Type.Currency['subunits'] { | ||||
if (config?.subunits) return config.subunits; | ||||
const dp = config?.decimalPlaces ?? defaultCurrency.decimalPlaces; | ||||
return Math.pow(10, dp); | ||||
} | ||||
|
||||
const defaultCurrency: Type.Currency = { | ||||
code: 'USD', | ||||
singularLabel: 'Dollar', | ||||
pluralLabel: 'Dollars', | ||||
sign: '$', | ||||
signB4: false, | ||||
decimalPlaces: 2, | ||||
decimalMark: '.', | ||||
thousandsSeparator: ',', | ||||
subunits: 100, // Math.pow(10, 2); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explanation at how we arrive at number There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right, based on the decimal places There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My interpretation of the default value came from here:
By default value, I mean when the |
||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { Type } from '.'; | ||
|
||
export const DateTimeFormats = (config?: Partial<Type.DateTimeFormats>): Type.DateTimeFormats => { | ||
const date = config?.dateFormat ?? defaultsFormats.date; | ||
const time = config?.timeFormat ?? defaultsFormats.time; | ||
const format = config?.dateTimeFormat ?? date + ' ' + time; | ||
|
||
return { | ||
dateFormat: date, | ||
timeFormat: time, | ||
dateTimeFormat: format, | ||
}; | ||
}; | ||
|
||
const defaultsFormats = { | ||
date: 'YYYY-MM-DD', | ||
time: 'HH:mm:ss', | ||
} as const; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { | ||
Type, | ||
EventEspressoData, | ||
BrandName, | ||
Currency, | ||
DateTimeFormats, | ||
Locale, | ||
Nonce, | ||
SiteUrls, | ||
Timezone, | ||
WpDebug, | ||
User, | ||
GeneralSettings, | ||
SitePermissions, | ||
} from '.'; | ||
|
||
export class Factory { | ||
constructor( | ||
private readonly config?: EventEspressoData['config'], | ||
private readonly api?: EventEspressoData['api'] | ||
) {} | ||
|
||
get = (): Type.Config => ({ | ||
brandName: BrandName(this.config?.coreDomain.brandName), | ||
currency: Currency(this.config?.siteCurrency), | ||
currentUser: User(this.config?.currentUser), | ||
generalSettings: GeneralSettings(this.config?.generalSettings), | ||
dateTimeFormats: DateTimeFormats(this.config?.generalSettings), | ||
locale: Locale(this.config?.locale), | ||
nonce: Nonce(this.api?.restApiNonce), | ||
sitePermissions: SitePermissions(this.config?.sitePermissions), | ||
siteUrl: SiteUrls(this.config?.siteUrls), | ||
timezone: Timezone(this.config?.locale.siteTimezone), | ||
wp_debug: WpDebug(this.config?.wp_debug), | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Type } from './types'; | ||
|
||
export function GeneralSettings(config?: Partial<Type.GeneralSettings>): Type.GeneralSettings { | ||
return { ...defaultSettings, ...config }; | ||
} | ||
|
||
const defaultSettings: Type.GeneralSettings = { | ||
dateFormat: '', | ||
timeFormat: '', | ||
timezone: '', | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Timezone, Type } from '.'; | ||
|
||
export const Locale = (config?: Partial<Type.Locale>): Type.Locale => ({ | ||
user: createLocale(config?.user ?? defaultLocale), | ||
site: createLocale(config?.site ?? defaultLocale), | ||
siteTimezone: Timezone(config?.siteTimezone), | ||
}); | ||
|
||
const defaultLocale = 'en-US'; | ||
|
||
function createLocale(string: string): string { | ||
return string.replace('_', '-'); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { Type } from '.'; | ||
|
||
export function Nonce(config?: Type.Nonce) { | ||
return config ?? ''; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { Type } from '.'; | ||
|
||
export function SitePermissions(config?: Type.SitePermissions): Type.SitePermissions { | ||
return config ?? []; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { Type } from '.'; | ||
|
||
export const SiteUrls = (config?: Partial<Type.SiteUrls>): Type.SiteUrls => ({ | ||
...defaultUrls, | ||
...config, | ||
}); | ||
|
||
const defaultUrls: Type.SiteUrls = { | ||
admin: '', | ||
home: '', | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { Type } from '.'; | ||
|
||
export const Timezone = (config?: Partial<Type.Timezone>): Type.Timezone => ({ | ||
...defaultTimezone, | ||
...config, | ||
}); | ||
|
||
const defaultTimezone: Type.Timezone = { | ||
city: 'UTC', | ||
name: 'UTC', | ||
offset: 0, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { Type } from '.'; | ||
|
||
export function User(user?: Type.User) { | ||
return user ?? defaultUser; | ||
} | ||
|
||
const defaultUser: Type.User = { | ||
capabilities: [], | ||
description: '', | ||
email: '', | ||
firstName: '', | ||
id: '', | ||
lastName: '', | ||
locale: '', | ||
name: '', | ||
nicename: '', | ||
nickname: '', | ||
username: '', | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { Type } from '.'; | ||
|
||
export function WpDebug(bool?: Type.WpDebug) { | ||
return bool ?? false; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export * from './types'; | ||
|
||
export * from './Currency'; | ||
export * from './DateTimeFormats'; | ||
export * from './Locale'; | ||
export * from './SiteUrls'; | ||
export * from './Timezone'; | ||
export * from './BrandName'; | ||
export * from './Factory'; | ||
export * from './Nonce'; | ||
export * from './WpDebug'; | ||
export * from './User'; | ||
export * from './GeneralSettings'; | ||
export * from './SitePermissions'; | ||
|
||
export * as test from './test'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { mockData } from './data'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './window'; | ||
export * from './types'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { User as _User } from '@eventespresso/data'; | ||
|
||
export module Type { | ||
export interface Currency { | ||
code: string; | ||
singularLabel: string; | ||
pluralLabel: string; | ||
sign: string; | ||
signB4: boolean; | ||
decimalPlaces: number; | ||
decimalMark: string; | ||
thousandsSeparator: string; | ||
subunits: number; | ||
} | ||
|
||
export interface DateTimeFormats { | ||
dateFormat: string; | ||
timeFormat: string; | ||
dateTimeFormat: string; | ||
} | ||
|
||
export interface Locale { | ||
user: string; | ||
site: string; | ||
siteTimezone: Timezone; | ||
} | ||
|
||
export interface SiteUrls { | ||
admin: string; | ||
home: string; | ||
} | ||
|
||
export interface Timezone { | ||
city: string; | ||
name: string; | ||
offset: number; | ||
} | ||
|
||
export interface GeneralSettings { | ||
dateFormat: string; | ||
timeFormat: string; | ||
timezone: string; | ||
} | ||
|
||
export interface Config { | ||
brandName: BrandName; | ||
currency: Currency; | ||
currentUser: User; // LATER: consolidate data types | ||
dateTimeFormats: DateTimeFormats; | ||
generalSettings: GeneralSettings; | ||
locale: Locale; | ||
nonce: Nonce; | ||
sitePermissions?: SitePermissions; | ||
siteUrl: SiteUrls; | ||
timezone: Timezone; | ||
wp_debug: WpDebug; | ||
} | ||
|
||
export type BrandName = string; | ||
|
||
export type Nonce = string; | ||
|
||
export type WpDebug = boolean; | ||
|
||
export type User = _User; | ||
|
||
export type SitePermissions = string[]; // LATER: consolidate data types | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a separate GH issue now for this and related
LATER
s - #1301