brackets2dots
Version:
Convert string with bracket notation to dot property notation for Node.js and the browser.
46 lines (39 loc) • 1.58 kB
JavaScript
!function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.brackets2dots=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
;
/*!
* 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, '')
: ''
}
},{}]},{},[1])
(1)
});