Skip to content
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

Support for decimal.js. Support for Prisma.Decimal. #158

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ Superjson supports many extra types which JSON does not. You can serialize all t
| `Set` | ❌ | ✅ |
| `Map` | ❌ | ✅ |
| `Error` | ❌ | ✅ |
| `Decimal` | ❌ | ✅ |

## Contributors ✨

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
},
"dependencies": {
"debug": "^4.3.1",
"decimal.js": "^10.3.1",
"lodash.clonedeep": "^4.5.0"
},
"resolutions": {
Expand Down
15 changes: 15 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from './is';

import { ObjectID } from 'mongodb';
import Decimal from 'decimal.js';

const isNode10 = process.version.indexOf('v10') === 0;

Expand Down Expand Up @@ -192,6 +193,20 @@ describe('stringify & parse', () => {
},
},

'works for Decimals': {
input: {
totalCost: new Decimal('199.99'),
},
output: {
totalCost: new Decimal('199.99').toJSON(),
},
outputAnnotations: {
values: {
totalCost: ['Decimal'],
},
},
},

'works for Errors': {
input: {
e: new Error('epic fail'),
Expand Down
4 changes: 4 additions & 0 deletions src/is.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Decimal from 'decimal.js';
import {
isArray,
isBoolean,
Expand All @@ -11,6 +12,7 @@ import {
isUndefined,
isPlainObject,
isTypedArray,
isDecimal,
} from './is';

test('Basic true tests', () => {
Expand All @@ -29,6 +31,7 @@ test('Basic true tests', () => {
expect(isNumber(0)).toBe(true);
expect(isNumber(1)).toBe(true);
expect(isDate(new Date())).toBe(true);
expect(isDecimal(new Decimal('100.1'))).toBe(true);
expect(isSymbol(Symbol())).toBe(true);
expect(isTypedArray(new Uint8Array())).toBe(true);
});
Expand All @@ -37,6 +40,7 @@ test('Basic false tests', () => {
expect(isNumber(NaN)).toBe(false);
expect(isDate(new Date('_'))).toBe(false);
expect(isDate(NaN)).toBe(false);
expect(isDecimal(NaN)).toBe(false);
expect(isUndefined(NaN)).toBe(false);
expect(isNull(NaN)).toBe(false);

Expand Down
5 changes: 5 additions & 0 deletions src/is.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Decimal from 'decimal.js';

const getType = (payload: any): string =>
Object.prototype.toString.call(payload).slice(8, -1);

Expand Down Expand Up @@ -84,3 +86,6 @@ export type TypedArray = InstanceType<TypedArrayConstructor>;

export const isTypedArray = (payload: any): payload is TypedArray =>
ArrayBuffer.isView(payload) && !(payload instanceof DataView);

export const isDecimal = (payload: any): payload is Decimal =>
Decimal.isDecimal(payload);
11 changes: 9 additions & 2 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import {
isArray,
isError,
isTypedArray,
isDecimal,
TypedArrayConstructor,
} from './is';
import { ClassRegistry } from './class-registry';
import { SymbolRegistry } from './symbol-registry';
import { CustomTransformerRegistry } from './custom-transformer-registry';
import { allowedErrorProps } from './error-props';
import { findArr } from './util';
import Decimal from 'decimal.js';

export type PrimitiveTypeAnnotation = 'number' | 'undefined' | 'bigint';

Expand All @@ -28,7 +30,7 @@ type ClassTypeAnnotation = ['class', string];
type SymbolTypeAnnotation = ['symbol', string];
type CustomTypeAnnotation = ['custom', string];

type SimpleTypeAnnotation = LeafTypeAnnotation | 'map' | 'set';
type SimpleTypeAnnotation = LeafTypeAnnotation | 'map' | 'set' | 'Decimal';

type CompositeTypeAnnotation =
| TypedArrayAnnotation
Expand Down Expand Up @@ -79,7 +81,12 @@ const simpleRules = [
v => v.toISOString(),
v => new Date(v)
),

simpleTransformation(
isDecimal,
'Decimal',
v => v.toJSON(),
v => new Decimal(v)
),
simpleTransformation(
isError,
'Error',
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TypeAnnotation } from './transformer';
import { MinimisedTree, ReferentialEqualityAnnotations } from './plainer';
import Decimal from 'decimal.js';

export type Class = { new (...args: any[]): any };

Expand All @@ -23,7 +24,8 @@ export type SerializableJSONValue =
| bigint
| Date
| ClassInstance
| RegExp;
| RegExp
| typeof Decimal;

export type SuperJSONValue =
| JSONValue
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2507,6 +2507,11 @@ decimal.js@^10.2.0:
resolved "https://registry.yarnpkg.com/decimal.js/-/decimal.js-10.2.1.tgz#238ae7b0f0c793d3e3cea410108b35a2c01426a3"
integrity sha512-KaL7+6Fw6i5A2XSnsbhm/6B+NuEA7TZ4vqxnd5tXz9sbKtrN9Srj8ab4vKVdK8YAqZO9P1kg45Y6YLoduPf+kw==

decimal.js@^10.3.1:
version "10.3.1"
resolved "https://registry.yarnpkg.com/decimal.js/-/decimal.js-10.3.1.tgz#d8c3a444a9c6774ba60ca6ad7261c3a94fd5e783"
integrity sha512-V0pfhfr8suzyPGOx3nmq4aHqabehUZn6Ch9kyFpV79TGDTWFmHqUqXdabR7QHqxzrYolF4+tVmJhUG4OURg5dQ==

decode-uri-component@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/decode-uri-component/-/decode-uri-component-0.2.0.tgz"
Expand Down