-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (35 loc) · 810 Bytes
/
index.js
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
'use strict';
/*!
* exports.
*/
module.exports = brackets2dots;
/*!
* regexp patterns.
*/
var REPLACE_BRACKETS = /\[([^\[\]]+)\]/g;
var LFT_RT_TRIM_DOTS = /^[.]*|[.]*$/g;
/**
* Convert string with bracket notation to dot property notation.
*
* ### Examples:
*
* brackets2dots('group[0].section.a.seat[3]')
* //=> 'group.0.section.a.seat.3'
*
* brackets2dots('[0].section.a.seat[3]')
* //=> '0.section.a.seat.3'
*
* brackets2dots('people[*].age')
* //=> 'people.*.age'
*
* @param {String} string
* original string
*
* @return {String}
* dot/bracket-notation string
*/
function brackets2dots(string) {
return ({}).toString.call(string) == '[object String]'
? string.replace(REPLACE_BRACKETS, '.$1').replace(LFT_RT_TRIM_DOTS, '')
: ''
}