Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 91 lines (60 sloc) 2.508 kb
9ae31f0 @beatgammit no radix means a float is assumed; added tests to check that
authored
1 TL;DR
2 =====
3
4 **Node**
5
6 `npm install cast`
7
8 **Ender**
9
10 `ender add cast`
11
12 **Usage**
13
14 var cast = require('cast');
15
16 cast('false', 'boolean'); // will return the boolean false
17
18 Intro
19 =====
20
21 Types in JavaScript can be unpredictable, and trying to protect against the edge-cases is a headache. Consider the following:
22
23 **Booleans**
24
25 > true === "true" // makes sense
26 > false !== "false" // ??
27 > false === "" // ??
28 > true === "0" // ?? or any string
29
30 **Numbers**
31
32 > isNaN(null) // false
33 > isNaN(undefined) // true
34
35 There are many others, but these conditions make it very difficult to write truly high-level code. This module tries to solve this problem.
36
37 The main use case is simplification of form validation/user input. A single call to cast will eliminate the need to do extensive type-checking.
38
39 Usage
40 =====
41
42 *function cast(val, type[, radix])*
43
367cd9e @beatgammit Replaced number with integer; version bump
authored
44 * val- the value to cast (strings are the most useful, but integers and arrays can also make sense)
9ae31f0 @beatgammit no radix means a float is assumed; added tests to check that
authored
45 * type- string or function (anything else will throw an exception)
46 * if a function, an `instanceof` will be done internally
13e922c @beatgammit Added support for negative numbers; added alias 'number' for 'float'; ve...
authored
47 * if a string, acceptable values are: 'array', 'boolean', 'float', 'integer', 'number' (alias for 'float')
367cd9e @beatgammit Replaced number with integer; version bump
authored
48 * radix- only applies for integers
9ae31f0 @beatgammit no radix means a float is assumed; added tests to check that
authored
49
50 There are only two possible return values, `null` or something of the type specified by type. Since null cannot be assigned to (as undefined can), this makes complete sense. The first parameter is never modified, so this function truly does no evil.
51
52 Examples
53 --------
54
55 **Arrays**
56
57 > cast([], 'array'); // returns [] (same reference)
58 > cast('[]', 'array'); // returns [] (from JSON.parse)
59
60 **Booleans**
61
62 > cast('true', 'boolean'); // returns true
63 > cast('false', 'boolean'); // returns false
64 > cast(true, 'boolean'); // returns true
65 > cast(false, 'boolean'); // returns false
66
367cd9e @beatgammit Replaced number with integer; version bump
authored
67 **Integers**
9ae31f0 @beatgammit no radix means a float is assumed; added tests to check that
authored
68
367cd9e @beatgammit Replaced number with integer; version bump
authored
69 > cast('10', 'integer'); // returns 10
70 > cast('10c', 'integer'); // returns null
71 > cast('10e10', 'integer'); // returns null
72 > cast(10, 'integer'); // returns 10
73 > cast('10', 'integer', 8); // returns 8
74 > cast('10', 'integer', '8'); // returns 8
13e922c @beatgammit Added support for negative numbers; added alias 'number' for 'float'; ve...
authored
75 > cast('10.1', 'integer', 10); // returns null
367cd9e @beatgammit Replaced number with integer; version bump
authored
76 > cast('10.1', 'integer'); // returns null
13e922c @beatgammit Added support for negative numbers; added alias 'number' for 'float'; ve...
authored
77
78 **Floats**
79
80 > cast('5.25', 'float'); // returns 5.25
7ae868a @beatgammit Added string casting
authored
81
82 **Strings**
83
84 > cast(5, 'string'); // returns '5'
85 > cast(true, 'string'; // returns 'true'
86 > cast('hello', 'string'); // returns 'hello'
87 > cast({}, 'string'); // returns null
88 > cast(undefined, 'string'); // returns null
89 > cast(null, 'string'); // returns null
90 > cast([], 'string'); // returns null
Something went wrong with that request. Please try again.