@nixxie-cms/fields-currency 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nixxie International DMCC
4
+ Portions Copyright (c) 2023 Thinkmill Labs Pty Ltd and contributors
5
+ (this software is derived from the KeystoneJS project, https://keystonejs.com)
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,14 @@
1
+ # @nixxie-cms/fields-currency
2
+
3
+ A money field for Nixxie CMS. Stores the amount in **minor units** (e.g. cents) as an integer to
4
+ avoid floating-point errors, plus a `formatCurrency` helper for display.
5
+
6
+ ```ts
7
+ import { currency, formatCurrency } from '@nixxie-cms/fields-currency'
8
+
9
+ fields: {
10
+ price: currency({ currency: 'USD' }),
11
+ }
12
+
13
+ formatCurrency(1999, 'USD') // "$19.99"
14
+ ```
@@ -0,0 +1,24 @@
1
+ import type { IntegerFieldConfig } from '@nixxie-cms/core/fields';
2
+ import type { BaseListTypeInfo, FieldTypeFunc } from '@nixxie-cms/core/types';
3
+ export type CurrencyFieldConfig<ListTypeInfo extends BaseListTypeInfo> = Omit<IntegerFieldConfig<ListTypeInfo>, 'validation' | 'defaultValue'> & {
4
+ /** ISO 4217 currency code stored only as documentation/metadata, e.g. 'USD'. Default: 'USD' */
5
+ currency?: string;
6
+ /** Allow negative amounts (e.g. refunds/credits). Default: false */
7
+ allowNegative?: boolean;
8
+ defaultValue?: number | null;
9
+ validation?: {
10
+ isRequired?: boolean;
11
+ };
12
+ };
13
+ /**
14
+ * A monetary field storing the amount in **minor units** (e.g. cents) as an integer, which avoids
15
+ * floating-point rounding errors. Format for display on the frontend with `Intl.NumberFormat`.
16
+ */
17
+ export declare function currency<ListTypeInfo extends BaseListTypeInfo>(config?: CurrencyFieldConfig<ListTypeInfo>): FieldTypeFunc<ListTypeInfo>;
18
+ /**
19
+ * Helper: format a minor-unit amount for display. When `minorUnitDigits` is omitted it is derived
20
+ * from the currency (2 for USD/EUR, 0 for JPY/KRW, 3 for KWD/BHD, …) so the divisor is correct for
21
+ * currencies whose minor unit is not 1/100.
22
+ */
23
+ export declare function formatCurrency(minorUnits: number, currency?: string, locale?: string, minorUnitDigits?: number): string;
24
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"../../../src","sources":["index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AACjE,OAAO,KAAK,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AAE7E,MAAM,MAAM,mBAAmB,CAAC,YAAY,SAAS,gBAAgB,IAAI,IAAI,CAC3E,kBAAkB,CAAC,YAAY,CAAC,EAChC,YAAY,GAAG,cAAc,CAC9B,GAAG;IACF,+FAA+F;IAC/F,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,oEAAoE;IACpE,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC5B,UAAU,CAAC,EAAE;QAAE,UAAU,CAAC,EAAE,OAAO,CAAA;KAAE,CAAA;CACtC,CAAA;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,YAAY,SAAS,gBAAgB,EAC5D,MAAM,GAAE,mBAAmB,CAAC,YAAY,CAAM,GAC7C,aAAa,CAAC,YAAY,CAAC,CAY7B;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAC5B,UAAU,EAAE,MAAM,EAClB,QAAQ,SAAQ,EAChB,MAAM,CAAC,EAAE,MAAM,EACf,eAAe,CAAC,EAAE,MAAM,GACvB,MAAM,CAIR"}
@@ -0,0 +1,2 @@
1
+ export * from "./declarations/src/index.js";
2
+ //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibml4eGllLWNtcy1maWVsZHMtY3VycmVuY3kuY2pzLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuL2RlY2xhcmF0aW9ucy9zcmMvaW5kZXguZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9
@@ -0,0 +1,49 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var fields = require('@nixxie-cms/core/fields');
6
+
7
+ /**
8
+ * A monetary field storing the amount in **minor units** (e.g. cents) as an integer, which avoids
9
+ * floating-point rounding errors. Format for display on the frontend with `Intl.NumberFormat`.
10
+ */
11
+ function currency(config = {}) {
12
+ const {
13
+ currency = 'USD',
14
+ allowNegative = false,
15
+ defaultValue,
16
+ validation,
17
+ ...rest
18
+ } = config;
19
+ return fields.integer({
20
+ ...rest,
21
+ defaultValue: defaultValue !== null && defaultValue !== void 0 ? defaultValue : null,
22
+ validation: {
23
+ isRequired: validation === null || validation === void 0 ? void 0 : validation.isRequired,
24
+ min: allowNegative ? undefined : 0
25
+ },
26
+ ui: {
27
+ description: `Amount in minor units of ${currency} (e.g. cents)`,
28
+ ...rest.ui
29
+ }
30
+ });
31
+ }
32
+
33
+ /**
34
+ * Helper: format a minor-unit amount for display. When `minorUnitDigits` is omitted it is derived
35
+ * from the currency (2 for USD/EUR, 0 for JPY/KRW, 3 for KWD/BHD, …) so the divisor is correct for
36
+ * currencies whose minor unit is not 1/100.
37
+ */
38
+ function formatCurrency(minorUnits, currency = 'USD', locale, minorUnitDigits) {
39
+ var _ref;
40
+ const formatter = new Intl.NumberFormat(locale, {
41
+ style: 'currency',
42
+ currency
43
+ });
44
+ const digits = (_ref = minorUnitDigits !== null && minorUnitDigits !== void 0 ? minorUnitDigits : formatter.resolvedOptions().maximumFractionDigits) !== null && _ref !== void 0 ? _ref : 2;
45
+ return formatter.format(minorUnits / 10 ** digits);
46
+ }
47
+
48
+ exports.currency = currency;
49
+ exports.formatCurrency = formatCurrency;
@@ -0,0 +1,44 @@
1
+ import { integer } from '@nixxie-cms/core/fields';
2
+
3
+ /**
4
+ * A monetary field storing the amount in **minor units** (e.g. cents) as an integer, which avoids
5
+ * floating-point rounding errors. Format for display on the frontend with `Intl.NumberFormat`.
6
+ */
7
+ function currency(config = {}) {
8
+ const {
9
+ currency = 'USD',
10
+ allowNegative = false,
11
+ defaultValue,
12
+ validation,
13
+ ...rest
14
+ } = config;
15
+ return integer({
16
+ ...rest,
17
+ defaultValue: defaultValue !== null && defaultValue !== void 0 ? defaultValue : null,
18
+ validation: {
19
+ isRequired: validation === null || validation === void 0 ? void 0 : validation.isRequired,
20
+ min: allowNegative ? undefined : 0
21
+ },
22
+ ui: {
23
+ description: `Amount in minor units of ${currency} (e.g. cents)`,
24
+ ...rest.ui
25
+ }
26
+ });
27
+ }
28
+
29
+ /**
30
+ * Helper: format a minor-unit amount for display. When `minorUnitDigits` is omitted it is derived
31
+ * from the currency (2 for USD/EUR, 0 for JPY/KRW, 3 for KWD/BHD, …) so the divisor is correct for
32
+ * currencies whose minor unit is not 1/100.
33
+ */
34
+ function formatCurrency(minorUnits, currency = 'USD', locale, minorUnitDigits) {
35
+ var _ref;
36
+ const formatter = new Intl.NumberFormat(locale, {
37
+ style: 'currency',
38
+ currency
39
+ });
40
+ const digits = (_ref = minorUnitDigits !== null && minorUnitDigits !== void 0 ? minorUnitDigits : formatter.resolvedOptions().maximumFractionDigits) !== null && _ref !== void 0 ? _ref : 2;
41
+ return formatter.format(minorUnits / 10 ** digits);
42
+ }
43
+
44
+ export { currency, formatCurrency };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@nixxie-cms/fields-currency",
3
+ "version": "1.0.1",
4
+ "license": "MIT",
5
+ "main": "dist/nixxie-cms-fields-currency.cjs.js",
6
+ "module": "dist/nixxie-cms-fields-currency.esm.js",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/nixxie-cms-fields-currency.cjs.js",
10
+ "module": "./dist/nixxie-cms-fields-currency.esm.js",
11
+ "default": "./dist/nixxie-cms-fields-currency.cjs.js"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
15
+ "dependencies": {
16
+ "@babel/runtime": "^7.24.7"
17
+ },
18
+ "devDependencies": {
19
+ "@nixxie-cms/core": "^1.0.1"
20
+ },
21
+ "peerDependencies": {
22
+ "@nixxie-cms/core": "^1.0.1"
23
+ },
24
+ "preconstruct": {
25
+ "entrypoints": [
26
+ "index.ts"
27
+ ]
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/nixxiecms/nixxie/tree/main/packages/fields-currency"
32
+ }
33
+ }
package/src/index.ts ADDED
@@ -0,0 +1,51 @@
1
+ import { integer } from '@nixxie-cms/core/fields'
2
+ import type { IntegerFieldConfig } from '@nixxie-cms/core/fields'
3
+ import type { BaseListTypeInfo, FieldTypeFunc } from '@nixxie-cms/core/types'
4
+
5
+ export type CurrencyFieldConfig<ListTypeInfo extends BaseListTypeInfo> = Omit<
6
+ IntegerFieldConfig<ListTypeInfo>,
7
+ 'validation' | 'defaultValue'
8
+ > & {
9
+ /** ISO 4217 currency code stored only as documentation/metadata, e.g. 'USD'. Default: 'USD' */
10
+ currency?: string
11
+ /** Allow negative amounts (e.g. refunds/credits). Default: false */
12
+ allowNegative?: boolean
13
+ defaultValue?: number | null
14
+ validation?: { isRequired?: boolean }
15
+ }
16
+
17
+ /**
18
+ * A monetary field storing the amount in **minor units** (e.g. cents) as an integer, which avoids
19
+ * floating-point rounding errors. Format for display on the frontend with `Intl.NumberFormat`.
20
+ */
21
+ export function currency<ListTypeInfo extends BaseListTypeInfo>(
22
+ config: CurrencyFieldConfig<ListTypeInfo> = {}
23
+ ): FieldTypeFunc<ListTypeInfo> {
24
+ const { currency = 'USD', allowNegative = false, defaultValue, validation, ...rest } = config
25
+
26
+ return integer<ListTypeInfo>({
27
+ ...(rest as IntegerFieldConfig<ListTypeInfo>),
28
+ defaultValue: defaultValue ?? null,
29
+ validation: {
30
+ isRequired: validation?.isRequired,
31
+ min: allowNegative ? undefined : 0,
32
+ },
33
+ ui: { description: `Amount in minor units of ${currency} (e.g. cents)`, ...rest.ui },
34
+ })
35
+ }
36
+
37
+ /**
38
+ * Helper: format a minor-unit amount for display. When `minorUnitDigits` is omitted it is derived
39
+ * from the currency (2 for USD/EUR, 0 for JPY/KRW, 3 for KWD/BHD, …) so the divisor is correct for
40
+ * currencies whose minor unit is not 1/100.
41
+ */
42
+ export function formatCurrency(
43
+ minorUnits: number,
44
+ currency = 'USD',
45
+ locale?: string,
46
+ minorUnitDigits?: number
47
+ ): string {
48
+ const formatter = new Intl.NumberFormat(locale, { style: 'currency', currency })
49
+ const digits = minorUnitDigits ?? formatter.resolvedOptions().maximumFractionDigits ?? 2
50
+ return formatter.format(minorUnits / 10 ** digits)
51
+ }