fl-builder-libs.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * JSONfn - javascript (both node.js and browser) plugin to stringify,
  3. * parse and clone objects with Functions, Regexp and Date.
  4. *
  5. * Version - 1.1.0
  6. * Copyright (c) Vadim Kiryukhin
  7. * vkiryukhin @ gmail.com
  8. * http://www.eslinstructor.net/jsonfn/
  9. *
  10. * Licensed under the MIT license ( http://www.opensource.org/licenses/mit-license.php )
  11. *
  12. * USAGE:
  13. * browser:
  14. * JSONfn.stringify(obj);
  15. * JSONfn.parse(str[, date2obj]);
  16. * JSONfn.clone(obj[, date2obj]);
  17. *
  18. * nodejs:
  19. * var JSONfn = require('path/to/json-fn');
  20. * JSONfn.stringify(obj);
  21. * JSONfn.parse(str[, date2obj]);
  22. * JSONfn.clone(obj[, date2obj]);
  23. *
  24. *
  25. * @obj - Object;
  26. * @str - String, which is returned by JSONfn.stringify() function;
  27. * @date2obj - Boolean (optional); if true, date string in ISO8061 format
  28. * is converted into a Date object; otherwise, it is left as a String.
  29. */
  30. (function (exports) {
  31. "use strict";
  32. exports.stringify = function (obj) {
  33. return JSON.stringify(obj, function (key, value) {
  34. var fnBody;
  35. if (value instanceof Function || typeof value == 'function') {
  36. fnBody = value.toString();
  37. if (fnBody.length < 8 || fnBody.substring(0, 8) !== 'function') { //this is ES6 Arrow Function
  38. return '_NuFrRa_' + fnBody;
  39. }
  40. return fnBody;
  41. }
  42. if (value instanceof RegExp) {
  43. return '_PxEgEr_' + value;
  44. }
  45. return value;
  46. });
  47. };
  48. exports.parse = function (str, date2obj) {
  49. var iso8061 = date2obj ? /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/ : false;
  50. return JSON.parse(str, function (key, value) {
  51. var prefix;
  52. if (typeof value != 'string') {
  53. return value;
  54. }
  55. if (value.length < 8) {
  56. return value;
  57. }
  58. prefix = value.substring(0, 8);
  59. if (iso8061 && value.match(iso8061)) {
  60. return new Date(value);
  61. }
  62. if (prefix === 'function') {
  63. return eval('(' + value + ')');
  64. }
  65. if (prefix === '_PxEgEr_') {
  66. return eval(value.slice(8));
  67. }
  68. if (prefix === '_NuFrRa_') {
  69. return eval(value.slice(8));
  70. }
  71. return value;
  72. });
  73. };
  74. exports.clone = function (obj, date2obj) {
  75. return exports.parse(exports.stringify(obj), date2obj);
  76. };
  77. }(typeof exports === 'undefined' ? (window.JSONfn = {}) : exports));
  78. function fl_slugify($attribute) {
  79. let string = encodeURIComponent($attribute.toString().toLowerCase().trim().replace(/["',]/g, "").replace(/\s/g, '-')).toLowerCase();
  80. const a = 'àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìłḿñńǹňôöòóœøōõṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·/,:;'
  81. const b = 'aaaaaaaaaacccddeeeeeeeegghiiiiiilmnnnnooooooooprrsssssttuuuuuuuuuwxyyzzz-----'
  82. const p = new RegExp(a.split('').join('|'), 'g')
  83. return string.toString().toLowerCase()
  84. .replace(/\.+/g, '-') // Replace . with
  85. .replace(/\:+/g, '') // Replace : with blank space
  86. .replace(/\s+/g, '-') // Replace spaces with -
  87. .replace(p, c => b.charAt(a.indexOf(c))) // Replace special characters
  88. .replace(/&/g, '-and-') // Replace & with 'and'
  89. .replace(/[^\w\-]+/g, '') // Remove all non-word characters
  90. .replace(/\-\-+/g, '-') // Replace multiple - with single -
  91. .replace(/^-+/, '') // Trim - from start of text
  92. .replace(/-+$/, '') // Trim - from end of text
  93. }