Skip to content

Commit

Permalink
Remove useless parameter limit
Browse files Browse the repository at this point in the history
  • Loading branch information
sveyret committed Sep 22, 2018
1 parent 7aaf988 commit 6b9cbf6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@
},
"homepage": "https://github.com/sveyret/intl-ts",
"devDependencies": {
"@types/chai": "4.1.4",
"@types/mocha": "5.2.3",
"@types/chai": "4.1.5",
"@types/mocha": "5.2.5",
"chai": "4.1.2",
"mocha": "5.2.0",
"nyc": "12.0.2",
"nyc": "13.0.1",
"rimraf": "2.6.2",
"ts-node": "7.0.0",
"tslint": "5.10.0",
"typescript": "2.9.2"
"ts-node": "7.0.1",
"tslint": "5.11.0",
"typescript": "3.0.3"
}
}
41 changes: 33 additions & 8 deletions src/Intl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
import { expect } from 'chai'

import Intl, { LanguageMap, Messages } from '.'
import { allAvailables, eo, fr, fr_ca, langType, languageMap } from './LanguageMap.spec'
import {
allAvailables,
eo,
fr,
fr_ca,
langType,
languageMap,
} from './LanguageMap.spec'

describe('Intl', function() {
const lang: Intl<langType> = new Intl(languageMap)
Expand All @@ -16,7 +23,13 @@ describe('Intl', function() {
expect(lang.hello('me')).to.equal('Hello me')
})

const RESERVED = ['$preferences', '$languageMap', '$changePreferences', '$withPreferences', '$getMessageFunction']
const RESERVED = [
'$preferences',
'$languageMap',
'$changePreferences',
'$withPreferences',
'$getMessageFunction'
]
RESERVED.forEach(key => {
const messages: Messages = {}
messages[key] = 'forbidden'
Expand Down Expand Up @@ -54,14 +67,18 @@ describe('Intl', function() {
const newLang = new Intl(lang, ['fr_CA', 'eo', 'fr'], false)
expect(newLang.welcome()).to.equal(eo.welcome)
expect(newLang.$getMessageFunction('hello')).to.equal(fr_ca.hello)
expect(newLang.$getMessageFunction('showElementCount')).to.equal(fr.showElementCount)
expect(newLang.$getMessageFunction('showElementCount')).to.equal(
fr.showElementCount
)
})

it('must use preferred available message or fallback to more generic type', function() {
const newLang = new Intl(lang, ['eo', 'fr_CA'])
expect(newLang.welcome()).to.equal(eo.welcome)
expect(newLang.$getMessageFunction('hello')).to.equal(eo.hello)
expect(newLang.$getMessageFunction('showElementCount')).to.equal(fr.showElementCount)
expect(newLang.$getMessageFunction('showElementCount')).to.equal(
fr.showElementCount
)
})

it('must forget non existing language in preferences', function() {
Expand Down Expand Up @@ -106,14 +123,18 @@ describe('Intl', function() {
const newLang = lang.$withPreferences(['fr_CA', 'eo', 'fr'], false)
expect(newLang.welcome()).to.equal(eo.welcome)
expect(newLang.$getMessageFunction('hello')).to.equal(fr_ca.hello)
expect(newLang.$getMessageFunction('showElementCount')).to.equal(fr.showElementCount)
expect(newLang.$getMessageFunction('showElementCount')).to.equal(
fr.showElementCount
)
})

it('must use preferred available message or fallback to more generic type', function() {
const newLang = lang.$withPreferences(['eo', 'fr_CA'])
expect(newLang.welcome()).to.equal(eo.welcome)
expect(newLang.$getMessageFunction('hello')).to.equal(eo.hello)
expect(newLang.$getMessageFunction('showElementCount')).to.equal(fr.showElementCount)
expect(newLang.$getMessageFunction('showElementCount')).to.equal(
fr.showElementCount
)
})

it('must forget non existing language in preferences', function() {
Expand Down Expand Up @@ -162,14 +183,18 @@ describe('Intl', function() {
lang.$changePreferences(['fr_CA', 'eo', 'fr'], false)
expect(lang.welcome()).to.equal(eo.welcome)
expect(lang.$getMessageFunction('hello')).to.equal(fr_ca.hello)
expect(lang.$getMessageFunction('showElementCount')).to.equal(fr.showElementCount)
expect(lang.$getMessageFunction('showElementCount')).to.equal(
fr.showElementCount
)
})

it('must use preferred available message or fallback to more generic type', function() {
lang.$changePreferences(['eo', 'fr_CA'])
expect(lang.welcome()).to.equal(eo.welcome)
expect(lang.$getMessageFunction('hello')).to.equal(eo.hello)
expect(lang.$getMessageFunction('showElementCount')).to.equal(fr.showElementCount)
expect(lang.$getMessageFunction('showElementCount')).to.equal(
fr.showElementCount
)
})

it('must forget non existing language in preferences', function() {
Expand Down
2 changes: 1 addition & 1 deletion src/Intl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function formatPreferences(preferences: ReadonlyArray<string>, createGenerics: b
* @param languageMap The language map.
* @param formattedPreferences The already formatted preferences.
*/
function calculatePreferences(languageMap: LanguageMap<any>, formattedPreferences: string[]): string[] {
function calculatePreferences(languageMap: LanguageMap<Messages>, formattedPreferences: string[]): string[] {
const preferences: string[] = []
for (const preference of formattedPreferences) {
if (!preferences.includes(preference) && languageMap.contains(preference)) {
Expand Down
11 changes: 4 additions & 7 deletions src/Messages.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
type MessageDirect = string
type MessageParams<P1, P2, P3, P4, P5, P6> = (arg1: P1, arg2: P2, arg3: P3, arg4: P4, arg5: P5, arg6: P6) => string

/**
* A message seen as function.
*/
export type MessageFunction<T extends MessageDirect | MessageParams<any, any, any, any, any, any>> = T extends string
? () => string
: T
export type MessageFunction<
T extends string | ((...args: any[]) => string)
> = T extends string ? () => string : T

/**
* The available messages for a given language.
*/
export interface Messages {
[key: string]: MessageDirect | MessageParams<any, any, any, any, any, any>
[key: string]: string | ((...args: any[]) => string)
}

0 comments on commit 6b9cbf6

Please sign in to comment.