Skip to content

Commit

Permalink
Merge pull request #4 from SecJS/fix/len-update-utils-validations
Browse files Browse the repository at this point in the history
fix: Add validations and update @secjs/utils
  • Loading branch information
jlenon7 authored Nov 30, 2021
2 parents c2dce61 + cfca3a4 commit ba6db47
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 97 deletions.
131 changes: 106 additions & 25 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@secjs/intl",
"version": "1.0.4",
"version": "1.0.5",
"description": "",
"license": "MIT",
"author": "João Lenon",
Expand All @@ -16,7 +16,8 @@
"devDependencies": {
"@secjs/contracts": "1.1.6",
"@secjs/exceptions": "1.0.3",
"@secjs/utils": "1.3.8",
"@secjs/logger": "1.2.2",
"@secjs/utils": "1.4.5",
"@types/debug": "4.1.7",
"@types/jest": "27.0.1",
"@typescript-eslint/eslint-plugin": "4.31.0",
Expand Down
76 changes: 52 additions & 24 deletions src/Sntl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { parse } from 'path'
import { getFolders } from '@secjs/utils'
import { getFoldersSync } from './utils/getFoldersSync'
import { NotImplementedException } from '@secjs/exceptions'
import { Folder, Path } from '@secjs/utils'
import { NotFoundException, NotImplementedException } from "@secjs/exceptions";

export class Sntl {
private static _tempLocale: string
Expand All @@ -15,40 +13,36 @@ export class Sntl {
}

async load() {
const path = `${process.cwd()}/resources/locales`
const path = Path.locales()

const { folders } = await getFolders(path, true)
const { folders } = await new Folder(path).load({ withFileContent: true })

folders.forEach(folder => {
const filesMap = new Map()

folder.files.forEach(file => {
if (typeof file.value !== 'string') return

filesMap.set(parse(file.name).name, JSON.parse(file.value))
folder.files.forEach(f => {
filesMap.set(f.name, JSON.parse(f.content.toString()))
})

Sntl.locales.set(folder.path, filesMap)
Sntl.locales.set(folder.name, filesMap)
})

return this
}

loadSync() {
const path = `${process.cwd()}/resources/locales`
const path = Path.locales()

const { folders } = getFoldersSync(path, true)
const { folders } = new Folder(path).loadSync({ withFileContent: true })

folders.forEach(folder => {
const filesMap = new Map()

folder.files.forEach(file => {
if (typeof file.value !== 'string') return

filesMap.set(parse(file.name).name, JSON.parse(file.value))
folder.files.forEach(f => {
filesMap.set(f.name, JSON.parse(f.content.toString()))
})

Sntl.locales.set(folder.path, filesMap)
Sntl.locales.set(folder.name, filesMap)
})

return this
Expand Down Expand Up @@ -81,9 +75,21 @@ export class Sntl {
}

static list(key: string, fields?: any) {
const list = Sntl.locales
.get(Sntl._tempLocale || Sntl._defaultLocale)
.get(key)
const locale = Sntl.locales.get(Sntl._tempLocale || Sntl._defaultLocale)

if (!locale) {
throw new NotFoundException(
`Locale ${Sntl._tempLocale || Sntl._defaultLocale} not found`,
)
}

const list = locale.get(key)

if (!list) {
throw new NotFoundException(
`Key ${key} not found in ${Sntl._tempLocale || Sntl._defaultLocale}`,
)
}

if (fields) {
Object.keys(fields).forEach(k => {
Expand All @@ -99,14 +105,36 @@ export class Sntl {
static formatMessage(key: string, fields?: any): string {
const splitedKey = key.split('.')

const keys = Sntl.locales
.get(Sntl._tempLocale || Sntl._defaultLocale)
.get(splitedKey[0])
const locale = Sntl.locales.get(Sntl._tempLocale || Sntl._defaultLocale)

if (!locale) {
throw new NotFoundException(
`Locale ${Sntl._tempLocale || Sntl._defaultLocale} not found`,
)
}

const keys = locale.get(splitedKey[0])

if (!keys) {
throw new NotFoundException(
`File ${splitedKey[0]} not found in locale ${
Sntl._tempLocale || Sntl._defaultLocale
}`,
)
}

splitedKey.shift()

let message: string = keys[splitedKey.join('.')]

if (!message) {
throw new NotFoundException(
`Key ${splitedKey.join('.')} not found in locale ${
Sntl._tempLocale || Sntl._defaultLocale
}`,
)
}

if (fields) {
Object.keys(fields).forEach(k => {
message = message.replace(new RegExp(`{{\\s*${k}\\s*}}`), fields[k])
Expand Down
46 changes: 0 additions & 46 deletions src/utils/getFoldersSync.ts

This file was deleted.

Loading

0 comments on commit ba6db47

Please sign in to comment.