decimal128.js 828 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. const Decimal128Type = require('../types/decimal128');
  3. const assert = require('assert');
  4. module.exports = function castDecimal128(value) {
  5. if (value == null) {
  6. return value;
  7. }
  8. if (typeof value === 'object' && typeof value.$numberDecimal === 'string') {
  9. return Decimal128Type.fromString(value.$numberDecimal);
  10. }
  11. if (value instanceof Decimal128Type) {
  12. return value;
  13. }
  14. if (typeof value === 'string') {
  15. return Decimal128Type.fromString(value);
  16. }
  17. if (Buffer.isBuffer(value)) {
  18. return new Decimal128Type(value);
  19. }
  20. if (typeof value === 'number') {
  21. return Decimal128Type.fromString(String(value));
  22. }
  23. if (typeof value.valueOf === 'function' && typeof value.valueOf() === 'string') {
  24. return Decimal128Type.fromString(value.valueOf());
  25. }
  26. assert.ok(false);
  27. };