File size: 2,294 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict';

// Built-in types
var types = [
  'object',
  'number',
  'string',
  'symbol',
  'boolean',
  'date',
  'function', // Weird to expose this
];

function normalize(coercer, value) {
  var coercers = coercer;
  if (!Array.isArray(coercers)) {
    coercers = [coercer];
  }

  var ctx = this;
  var args = slice(arguments, 2);

  // Try in order until one returns a non-undefined value
  var result;
  coercers.some(function (coercer) {
    var val = value;
    if (typeof value === 'function' && coercer !== 'function') {
      val = value.apply(ctx, args);
    }

    result = coerce(ctx, coercer, val);
    return result !== undefined;
  });

  return result;
}

function coerce(ctx, coercer, value) {
  // Handle built-in types
  if (typeof coercer === 'string') {
    if (coerce[coercer]) {
      return coerce[coercer].call(ctx, value);
    }
    return typeOf(coercer, value);
  }

  // Handle custom coercer
  if (typeof coercer === 'function') {
    return coercer.call(ctx, value);
  }

  throw new Error('Invalid coercer. Can only be a string or function.');
}

coerce.string = function (value) {
  if (
    value != null &&
    typeof value === 'object' &&
    typeof value.toString === 'function'
  ) {
    value = value.toString();
  }
  return typeOf('string', primitive(value));
};

coerce.number = function (value) {
  return typeOf('number', primitive(value));
};

coerce.boolean = function (value) {
  return typeOf('boolean', primitive(value));
};

coerce.date = function (value) {
  value = primitive(value);
  if (typeof value === 'number' && !isNaN(value) && isFinite(value)) {
    return new Date(value);
  }
};

function typeOf(type, value) {
  if (typeof value === type) {
    return value;
  }
}

function primitive(value) {
  if (
    value != null &&
    typeof value === 'object' &&
    typeof value.valueOf === 'function'
  ) {
    value = value.valueOf();
  }
  return value;
}

function slice(value, from) {
  return Array.prototype.slice.call(value, from);
}

// Add methods for each type
types.forEach(function (type) {
  // Make it an array for easier concat
  var typeArg = [type];

  normalize[type] = function () {
    var args = slice(arguments);
    return normalize.apply(this, typeArg.concat(args));
  };
});

module.exports = normalize;