版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

267 строки
6.7 KiB

  1. import { isNonEmpty } from './ReadonlyNonEmptyArray';
  2. // -------------------------------------------------------------------------------------
  3. // instances
  4. // -------------------------------------------------------------------------------------
  5. /**
  6. * @example
  7. * import * as S from 'fp-ts/string'
  8. *
  9. * assert.deepStrictEqual(S.Eq.equals('a', 'a'), true)
  10. * assert.deepStrictEqual(S.Eq.equals('a', 'b'), false)
  11. *
  12. * @category instances
  13. * @since 2.10.0
  14. */
  15. export var Eq = {
  16. equals: function (first, second) { return first === second; }
  17. };
  18. /**
  19. * `string` semigroup under concatenation.
  20. *
  21. * @example
  22. * import * as S from 'fp-ts/string'
  23. *
  24. * assert.deepStrictEqual(S.Semigroup.concat('a', 'b'), 'ab')
  25. *
  26. * @category instances
  27. * @since 2.10.0
  28. */
  29. export var Semigroup = {
  30. concat: function (first, second) { return first + second; }
  31. };
  32. /**
  33. * An empty `string`.
  34. *
  35. * @since 2.10.0
  36. */
  37. export var empty = '';
  38. /**
  39. * `string` monoid under concatenation.
  40. *
  41. * The `empty` value is `''`.
  42. *
  43. * @example
  44. * import * as S from 'fp-ts/string'
  45. *
  46. * assert.deepStrictEqual(S.Monoid.concat('a', 'b'), 'ab')
  47. * assert.deepStrictEqual(S.Monoid.concat('a', S.Monoid.empty), 'a')
  48. *
  49. * @category instances
  50. * @since 2.10.0
  51. */
  52. export var Monoid = {
  53. concat: Semigroup.concat,
  54. empty: empty
  55. };
  56. /**
  57. * @example
  58. * import * as S from 'fp-ts/string'
  59. *
  60. * assert.deepStrictEqual(S.Ord.compare('a', 'a'), 0)
  61. * assert.deepStrictEqual(S.Ord.compare('a', 'b'), -1)
  62. * assert.deepStrictEqual(S.Ord.compare('b', 'a'), 1)
  63. *
  64. * @category instances
  65. * @since 2.10.0
  66. */
  67. export var Ord = {
  68. equals: Eq.equals,
  69. compare: function (first, second) { return (first < second ? -1 : first > second ? 1 : 0); }
  70. };
  71. /**
  72. * @example
  73. * import * as S from 'fp-ts/string'
  74. *
  75. * assert.deepStrictEqual(S.Show.show('a'), '"a"')
  76. *
  77. * @category instances
  78. * @since 2.10.0
  79. */
  80. export var Show = {
  81. show: function (s) { return JSON.stringify(s); }
  82. };
  83. // -------------------------------------------------------------------------------------
  84. // refinements
  85. // -------------------------------------------------------------------------------------
  86. /**
  87. * @example
  88. * import * as S from 'fp-ts/string'
  89. *
  90. * assert.deepStrictEqual(S.isString('a'), true)
  91. * assert.deepStrictEqual(S.isString(1), false)
  92. *
  93. * @category refinements
  94. * @since 2.11.0
  95. */
  96. export var isString = function (u) { return typeof u === 'string'; };
  97. // -------------------------------------------------------------------------------------
  98. // combinators
  99. // -------------------------------------------------------------------------------------
  100. /**
  101. * @example
  102. * import * as S from 'fp-ts/string'
  103. * import { pipe } from 'fp-ts/function'
  104. *
  105. * assert.deepStrictEqual(pipe('a', S.toUpperCase), 'A')
  106. *
  107. * @since 2.11.0
  108. */
  109. export var toUpperCase = function (s) { return s.toUpperCase(); };
  110. /**
  111. * @example
  112. * import * as S from 'fp-ts/string'
  113. * import { pipe } from 'fp-ts/function'
  114. *
  115. * assert.deepStrictEqual(pipe('A', S.toLowerCase), 'a')
  116. *
  117. * @since 2.11.0
  118. */
  119. export var toLowerCase = function (s) { return s.toLowerCase(); };
  120. /**
  121. * @example
  122. * import * as S from 'fp-ts/string'
  123. * import { pipe } from 'fp-ts/function'
  124. *
  125. * assert.deepStrictEqual(pipe('abc', S.replace('b', 'd')), 'adc')
  126. *
  127. * @since 2.11.0
  128. */
  129. export var replace = function (searchValue, replaceValue) {
  130. return function (s) {
  131. return s.replace(searchValue, replaceValue);
  132. };
  133. };
  134. /**
  135. * @example
  136. * import * as S from 'fp-ts/string'
  137. * import { pipe } from 'fp-ts/function'
  138. *
  139. * assert.deepStrictEqual(pipe(' a ', S.trim), 'a')
  140. *
  141. * @since 2.11.0
  142. */
  143. export var trim = function (s) { return s.trim(); };
  144. /**
  145. * @example
  146. * import * as S from 'fp-ts/string'
  147. * import { pipe } from 'fp-ts/function'
  148. *
  149. * assert.deepStrictEqual(pipe(' a ', S.trimLeft), 'a ')
  150. *
  151. * @since 2.11.0
  152. */
  153. export var trimLeft = function (s) { return s.trimLeft(); };
  154. /**
  155. * @example
  156. * import * as S from 'fp-ts/string'
  157. * import { pipe } from 'fp-ts/function'
  158. *
  159. * assert.deepStrictEqual(pipe(' a ', S.trimRight), ' a')
  160. *
  161. * @since 2.11.0
  162. */
  163. export var trimRight = function (s) { return s.trimRight(); };
  164. /**
  165. * @example
  166. * import * as S from 'fp-ts/string'
  167. * import { pipe } from 'fp-ts/function'
  168. *
  169. * assert.deepStrictEqual(pipe('abcd', S.slice(1, 3)), 'bc')
  170. *
  171. * @since 2.11.0
  172. */
  173. export var slice = function (start, end) {
  174. return function (s) {
  175. return s.slice(start, end);
  176. };
  177. };
  178. // -------------------------------------------------------------------------------------
  179. // utils
  180. // -------------------------------------------------------------------------------------
  181. /**
  182. * Test whether a `string` is empty.
  183. *
  184. * @example
  185. * import * as S from 'fp-ts/string'
  186. * import { pipe } from 'fp-ts/function'
  187. *
  188. * assert.deepStrictEqual(pipe('', S.isEmpty), true)
  189. * assert.deepStrictEqual(pipe('a', S.isEmpty), false)
  190. *
  191. * @since 2.10.0
  192. */
  193. export var isEmpty = function (s) { return s.length === 0; };
  194. /**
  195. * Calculate the number of characters in a `string`.
  196. *
  197. * @example
  198. * import * as S from 'fp-ts/string'
  199. * import { pipe } from 'fp-ts/function'
  200. *
  201. * assert.deepStrictEqual(pipe('abc', S.size), 3)
  202. *
  203. * @since 2.10.0
  204. */
  205. export var size = function (s) { return s.length; };
  206. /**
  207. * @example
  208. * import * as S from 'fp-ts/string'
  209. * import { pipe } from 'fp-ts/function'
  210. *
  211. * assert.deepStrictEqual(pipe('abc', S.split('')), ['a', 'b', 'c'])
  212. * assert.deepStrictEqual(pipe('', S.split('')), [''])
  213. *
  214. * @since 2.11.0
  215. */
  216. export var split = function (separator) {
  217. return function (s) {
  218. var out = s.split(separator);
  219. return isNonEmpty(out) ? out : [s];
  220. };
  221. };
  222. /**
  223. * @example
  224. * import * as S from 'fp-ts/string'
  225. * import { pipe } from 'fp-ts/function'
  226. *
  227. * assert.deepStrictEqual(pipe('abc', S.includes('b')), true)
  228. * assert.deepStrictEqual(pipe('abc', S.includes('d')), false)
  229. *
  230. * @since 2.11.0
  231. */
  232. export var includes = function (searchString, position) {
  233. return function (s) {
  234. return s.includes(searchString, position);
  235. };
  236. };
  237. /**
  238. * @example
  239. * import * as S from 'fp-ts/string'
  240. * import { pipe } from 'fp-ts/function'
  241. *
  242. * assert.deepStrictEqual(pipe('abc', S.startsWith('a')), true)
  243. * assert.deepStrictEqual(pipe('bc', S.startsWith('a')), false)
  244. *
  245. * @since 2.11.0
  246. */
  247. export var startsWith = function (searchString, position) {
  248. return function (s) {
  249. return s.startsWith(searchString, position);
  250. };
  251. };
  252. /**
  253. * @example
  254. * import * as S from 'fp-ts/string'
  255. * import { pipe } from 'fp-ts/function'
  256. *
  257. * assert.deepStrictEqual(pipe('abc', S.endsWith('c')), true)
  258. * assert.deepStrictEqual(pipe('ab', S.endsWith('c')), false)
  259. *
  260. * @since 2.11.0
  261. */
  262. export var endsWith = function (searchString, position) {
  263. return function (s) {
  264. return s.endsWith(searchString, position);
  265. };
  266. };