UNPKG

1.13 kBJavaScriptView Raw
1'use strict';
2
3var stripBom = require('strip-bom-string');
4exports.typeOf = require('kind-of');
5
6/**
7 * Returns true if `val` is a buffer
8 */
9
10exports.isBuffer = function(val) {
11 return exports.typeOf(val) === 'buffer';
12};
13
14/**
15 * Returns true if `val` is an object
16 */
17
18exports.isObject = function(val) {
19 return exports.typeOf(val) === 'object';
20};
21
22/**
23 * Cast `input` to a buffer
24 */
25
26exports.toBuffer = function(input) {
27 if (typeof input === 'string') {
28 return new Buffer(input);
29 }
30 return input;
31};
32
33/**
34 * Cast `val` to a string.
35 */
36
37exports.toString = function(input) {
38 if (exports.isBuffer(input)) {
39 return stripBom(String(input));
40 }
41 if (typeof input !== 'string') {
42 throw new TypeError('expected input to be a string or buffer');
43 }
44 return stripBom(input);
45};
46
47/**
48 * Cast `val` to an array.
49 */
50
51exports.arrayify = function(val) {
52 return val ? (Array.isArray(val) ? val : [val]) : [];
53};
54
55/**
56 * Returns true if `str` starts with `substr`.
57 */
58
59exports.startsWith = function(str, substr, len) {
60 if (typeof len !== 'number') len = substr.length;
61 return str.slice(0, len) === substr;
62};