strings.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'underscore'
  7. ], function (_) {
  8. 'use strict';
  9. var jsonRe = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/;
  10. return {
  11. /**
  12. * Attempts to convert string to one of the primitive values,
  13. * or to parse it as a valid json object.
  14. *
  15. * @param {String} str - String to be processed.
  16. * @returns {*}
  17. */
  18. castString: function (str) {
  19. try {
  20. str = str === 'true' ? true :
  21. str === 'false' ? false :
  22. str === 'null' ? null :
  23. +str + '' === str ? +str :
  24. jsonRe.test(str) ? JSON.parse(str) :
  25. str;
  26. } catch (e) {
  27. }
  28. return str;
  29. },
  30. /**
  31. * Splits string by separator if it's possible,
  32. * otherwise returns the incoming value.
  33. *
  34. * @param {(String|Array|*)} str - String to split.
  35. * @param {String} [separator=' '] - Seperator based on which to split the string.
  36. * @returns {Array|*} Splitted string or the incoming value.
  37. */
  38. stringToArray: function (str, separator) {
  39. separator = separator || ' ';
  40. return typeof str === 'string' ?
  41. str.split(separator) :
  42. str;
  43. },
  44. /**
  45. * Converts the incoming string which consists
  46. * of a specified delimiters into a format commonly used in form elements.
  47. *
  48. * @param {String} name - The incoming string.
  49. * @param {String} [separator='.']
  50. * @returns {String} Serialized string.
  51. *
  52. * @example
  53. * utils.serializeName('one.two.three');
  54. * => 'one[two][three]';
  55. */
  56. serializeName: function (name, separator) {
  57. var result;
  58. separator = separator || '.';
  59. name = name.split(separator);
  60. result = name.shift();
  61. name.forEach(function (part) {
  62. result += '[' + part + ']';
  63. });
  64. return result;
  65. },
  66. /**
  67. * Checks wether the incoming value is not empty,
  68. * e.g. not 'null' or 'undefined'
  69. *
  70. * @param {*} value - Value to check.
  71. * @returns {Boolean}
  72. */
  73. isEmpty: function (value) {
  74. return value === '' || _.isUndefined(value) || _.isNull(value);
  75. },
  76. /**
  77. * Adds 'prefix' to the 'part' value if it was provided.
  78. *
  79. * @param {String} prefix
  80. * @param {String} part
  81. * @returns {String}
  82. */
  83. fullPath: function (prefix, part) {
  84. return prefix ? prefix + '.' + part : part;
  85. },
  86. /**
  87. * Splits incoming string and returns its' part specified by offset.
  88. *
  89. * @param {String} parts
  90. * @param {Number} [offset]
  91. * @param {String} [delimiter=.]
  92. * @returns {String}
  93. */
  94. getPart: function (parts, offset, delimiter) {
  95. delimiter = delimiter || '.';
  96. parts = parts.split(delimiter);
  97. offset = this.formatOffset(parts, offset);
  98. parts.splice(offset, 1);
  99. return parts.join(delimiter) || '';
  100. },
  101. /**
  102. * Converts nameThroughCamelCase to name-through-minus
  103. *
  104. * @param {String} string
  105. * @returns {String}
  106. */
  107. camelCaseToMinus: function camelCaseToMinus(string) {
  108. return ('' + string)
  109. .split('')
  110. .map(function (symbol, index) {
  111. return index ?
  112. symbol.toUpperCase() === symbol ?
  113. '-' + symbol.toLowerCase() :
  114. symbol :
  115. symbol.toLowerCase();
  116. })
  117. .join('');
  118. },
  119. /**
  120. * Converts name-through-minus to nameThroughCamelCase
  121. *
  122. * @param {String} string
  123. * @returns {String}
  124. */
  125. minusToCamelCase: function minusToCamelCase(string) {
  126. return ('' + string)
  127. .split('-')
  128. .map(function (part, index) {
  129. return index ? part.charAt(0).toUpperCase() + part.slice(1) : part;
  130. })
  131. .join('');
  132. }
  133. };
  134. });