UNPKG

1.52 kBJavaScriptView Raw
1"use strict";
2
3module.exports = {
4 camelCase,
5 CamelCase,
6 isSpecial,
7 contains
8};
9
10
11const NOT_FOUND = -1;
12
13/**
14 * Does the array `arr` contains the element `item`?
15 *
16 * @param {array} arr - Array in which to look.
17 * @param {any} item - Item we are looking for.
18 * @returns {boolean} Return `true` is the array contains the given element.
19 */
20function contains( arr, item ) {
21 return arr.indexOf( item ) !== NOT_FOUND;
22}
23
24function camelCase( name ) {
25 return name.split( '.' ).map( function ( word, wordIdx ) {
26 return word.split( '-' ).map( function ( piece, pieceIdx ) {
27 if ( wordIdx + pieceIdx === 0 ) return piece;
28 return piece.charAt( 0 ).toUpperCase() + piece.substr( 1 );
29 } ).join( "" );
30 } ).join( "" );
31}
32
33function CamelCase( name ) {
34 var transformed = camelCase( name );
35 return transformed.charAt( 0 ).toUpperCase() + transformed.substr( 1 );
36}
37
38/**
39 * Determine if an object is special or not.
40 * An object is special as soon as it own the "0" attribute.
41 * @param {string=undefined} expectedName - _If defined, test the value of `obj[0]`.
42 */
43function isSpecial( obj, expectedName ) {
44 var type = typeof obj;
45 if ( type === 'string' || type !== 'object' || Array.isArray( obj ) ) return false;
46 if ( !obj ) return false;
47 var name = obj[ 0 ];
48
49 if ( typeof name !== 'string' ) return false;
50 if ( typeof expectedName === 'string' ) {
51 return name.toLowerCase() === expectedName.toLowerCase();
52 }
53 return true;
54}
\No newline at end of file