Skip to content

Commit

Permalink
Add reversed usage zm (#3)
Browse files Browse the repository at this point in the history
* Add reversed usage zm. Update README. Add zm tests

* Fixes zm implementation based on PR comments
  • Loading branch information
menghif authored Oct 11, 2021
1 parent 47145a2 commit 264f21a
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 23 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ const str = mz(1)`There {was|were} $value person{|s} at this event.`
const str = mz(5)`There {was|were} $value person{|s} at this event.`
// str = "There were 5 persons at this event."
```
#### Reversed usage
You can use the exported `zm` function to create a function curried with the template first and then calling it with the amount:
```ts
import { zm } from "mehrzahl"

const template = zm`There {was|were} $value person{|s} at this event.`
template(5) // There were 5 persons at this event.
template(0) // There was 1 person at this event.
```

💡 `$value` is replaced by the amount specified.

Expand Down
82 changes: 81 additions & 1 deletion src/__tests__/mehrzahl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mz } from "../mehrzahl"
import { mz, zm } from "../mehrzahl"

describe("Mehrzahl function", () => {
it("interpolates function expressions", () => {
Expand Down Expand Up @@ -78,3 +78,83 @@ describe("Mehrzahl function", () => {
expect(resultPlural).toEqual("number is plural (5)")
})
})

describe("ZM function", () => {
it("interpolates function expressions", () => {
const resultSingular = zm`number is ${(plural) =>
plural ? "plural" : "singular"}`(1)
const resultPlural = zm`number is ${(plural) =>
plural ? "plural" : "singular"}`(5)

expect(resultSingular).toEqual("number is singular")
expect(resultPlural).toEqual("number is plural")
})

it("interpolates singular/plural tuples", () => {
const resultSingular = zm`number is ${["singular", "plural"]}`(1)
const resultPlural = zm`number is ${["singular", "plural"]}`(5)

expect(resultSingular).toEqual("number is singular")
expect(resultPlural).toEqual("number is plural")
})

it("interpolates singular/plural tuples with undefined/null values", () => {
const formatSingular1 = zm`number is ${[null, "plural"]}`
const formatSingular2 = zm`number is ${["singular"]}`
const formatPlural1 = zm`number is ${[, "plural"]}`
const formatPlural2 = zm`number is ${["singular"]}`

expect(formatSingular1(1)).toEqual("number is ")
expect(formatSingular2(1)).toEqual("number is singular")
expect(formatPlural1(5)).toEqual("number is plural")
expect(formatPlural2(5)).toEqual("number is ")
})

it("interpolates singular/plural tuples and function expressions", () => {
const resultSingular = zm`number is ${["singular", "plural"]} ${(
plural
) => (plural ? "pluralFunction" : "singularFunction")}`(1)
const resultPlural = zm`number is ${["singular", "plural"]} ${(plural) =>
plural ? "pluralFunction" : "singularFunction"}`(5)

expect(resultSingular).toEqual("number is singular singularFunction")
expect(resultPlural).toEqual("number is plural pluralFunction")
})

it("transforms {<singular>|<plural>} syntax", () => {
const resultSingular = zm`number is {singular|plural}`(1)

const resultPlural = zm`number is {singular|plural}`(5)

expect(resultSingular).toEqual("number is singular")
expect(resultPlural).toEqual("number is plural")
})

it("transforms {<singular>|<plural>} syntax with empty strings", () => {
const resultSingular = zm`number is {singular|}`(1)

const resultPlural = zm`number is {|plural}`(5)

expect(resultSingular).toEqual("number is singular")
expect(resultPlural).toEqual("number is plural")
})

it("transforms nested groups", () => {
const resultSingular = zm`number is {singular ($value)|}`(1)

const resultPlural = zm`number is {|plural ($value)}`(5)

expect(resultSingular).toEqual("number is singular (1)")
expect(resultPlural).toEqual("number is plural (5)")
})

it("replaces $value", () => {
const resultSingular = zm`number is {singular ($value)|}`(1)

const resultPlural = zm`number is {|plural ($value)}`(5)

expect(resultSingular).toEqual("number is singular (1)")
expect(resultPlural).toEqual("number is plural (5)")
})

})
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { mz as default, mz } from "./mehrzahl"
export { mz as default, mz, zm } from "./mehrzahl"
79 changes: 58 additions & 21 deletions src/mehrzahl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ export type MehrzahlFormatterFactory = (
delimiter?: string
) => MehrzahlTaggedFormatter

export type ReversedMerzahlFormatter = (
amount: number,
delimiter?: string
) => string

export type ReversedMehrzahlFormatterFactory = (
strings: TemplateStringsArray,
...valuesToInterpolate: InterpolatableValue[]
) => ReversedMerzahlFormatter

export type InterpolationFunction = (plural: boolean, amount: number) => string

export type SingularPluralTuple = [
Expand Down Expand Up @@ -39,27 +49,54 @@ const formatGroupSyntax = (
export const mz: MehrzahlFormatterFactory =
(amount, delimiter = DEFAULT_DELIMITER) =>
(strings, ...valuesToInterpolate) => {
const isPlural = amount !== 1
const amountString = amount.toString()

const interpolatedValues = valuesToInterpolate.map((value) => {
if (typeof value === "function") {
return value(isPlural, amount)
}
return mehrzahl(
amount,
delimiter,
strings,
...valuesToInterpolate
)
}

if (Array.isArray(value)) {
return value[isPlural ? 1 : 0] ?? ""
}
return value
})
// reverse of mz
export const zm: ReversedMehrzahlFormatterFactory =
(strings, ...valuesToInterpolate) =>
(amount, delimiter = DEFAULT_DELIMITER) => {
return mehrzahl(
amount,
delimiter,
strings,
...valuesToInterpolate
)
}

return formatGroupSyntax(
strings.reduce(
(result, part, index) =>
result + part + (interpolatedValues[index] ?? ""),
""
),
isPlural,
delimiter
).replace("$value", amountString)
const mehrzahl = (
amount: number,
delimiter: string,
strings: TemplateStringsArray,
...valuesToInterpolate: InterpolatableValue[]
) => {
const isPlural = amount !== 1
const amountString = amount.toString()

const interpolatedValues = valuesToInterpolate.map((value) => {
if (typeof value === "function") {
return value(isPlural, amount)
}

if (Array.isArray(value)) {
return value[isPlural ? 1 : 0] ?? ""
}
return value
})

return formatGroupSyntax(
strings.reduce(
(result, part, index) =>
result + part + (interpolatedValues[index] ?? ""),
""
),
isPlural,
delimiter
).replace("$value", amountString)
}

0 comments on commit 264f21a

Please sign in to comment.