@xcrap/transformer 0.0.8 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,7 @@ export declare namespace StringTransformer {
6
6
  const replace: (searchValue: string | RegExp, replaceValue: string) => (value: string) => string;
7
7
  const remove: (searchValue: string | RegExp) => (value: string) => string;
8
8
  const split: (separator: string) => (value: string) => string[];
9
+ const extract: (pattern: RegExp, index?: number) => (value: string) => string;
9
10
  const toBoolean: (value: string) => boolean;
10
11
  const normalize: (value: string) => string;
11
12
  const removeHtmlTags: (value: string) => string;
@@ -11,6 +11,17 @@ var StringTransformer;
11
11
  StringTransformer.replace = (searchValue, replaceValue) => (value) => value.replace(searchValue, replaceValue);
12
12
  StringTransformer.remove = (searchValue) => (value) => value.replace(searchValue, "");
13
13
  StringTransformer.split = (separator) => (value) => value.split(separator);
14
+ StringTransformer.extract = (pattern, index = 0) => (value) => {
15
+ const match = value.match(pattern);
16
+ if (!match) {
17
+ throw new Error(`Pattern '${pattern}' did not match value '${value}'`);
18
+ }
19
+ const params = match[index];
20
+ if (!params) {
21
+ throw new Error(`Pattern '${pattern}' matched but index ${index} is out of bounds.`);
22
+ }
23
+ return params;
24
+ };
14
25
  StringTransformer.toBoolean = (value) => {
15
26
  if (!string_1.StringValidator.isBooleanText(value)) {
16
27
  throw new Error(`'${value}' cannot be converted to boolean!`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xcrap/transformer",
3
- "version": "0.0.8",
3
+ "version": "0.1.0",
4
4
  "description": "Xcrap Transformer is the data transformation package extracted from the Web Scraping framework Xcrap.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",