UNPKG

ngmap

Version:
36 lines (33 loc) 867 B
/** * @ngdoc filter * @name jsonize * @description * Converts json-like string to json string */ (function() { 'use strict'; var jsonizeFilter = function() { return function(str) { try { // if parsable already, return as it is JSON.parse(str); return str; } catch(e) { // if not parsable, change little return str // wrap keys without quote with valid double quote .replace(/([\$\w]+)\s*:/g, function(_, $1) { return '"'+$1+'":'; } ) // replacing single quote wrapped ones to double quote .replace(/'([^']+)'/g, function(_, $1) { return '"'+$1+'"'; } ) .replace(/''/g, '""'); } }; }; angular.module('ngMap').filter('jsonize', jsonizeFilter); })();