Skip to content

Commit

Permalink
feat(operators-change-case): BREAKING CHANGE Upgrade change-case depe…
Browse files Browse the repository at this point in the history
…ndency to 5.4.0.
  • Loading branch information
StephanieJKS committed Jan 10, 2024
1 parent 0e603d6 commit 5cfe04a
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 297 deletions.
22 changes: 22 additions & 0 deletions .changeset/real-keys-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'@lowdefy/operators-change-case': major
---

Upgrade change-case dependency to 5.4.0

This is a breaking change and effects the `_change_case` operator.

Changes to the `_change_case` operator:

- Options splitRegex and stripRegexp are no longer supported.
- paramCase has been renamed to kebabCase
- headerCase has been renamed to trainCase
- The following options have been added:

- locale
- mergeAmbiguousCharacters
- prefixCharacters
- split
- suffixCharacters

- Added pascalSnakeCase which transforms a string into a string of capitalized words with underscores between words.
34 changes: 34 additions & 0 deletions packages/docs/migration/v3-to-v4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,37 @@ _ref:
The `LOWDEFY_SERVER_PUBLIC_DIRECTORY` and `LOWDEFY_SERVER_BUILD_DIRECTORY` are no longer supported because configuration is bundled with the server at build time.
The `LOWDEFY_SERVER_PORT` environment variable has been replaced by the `PORT` environment variable.
## _change_case
The options splitRegex and stripRegex are no longer supported in v4 and the following have been renamed:
- paramCase to kebabCase
- headerCase to trainCase
###### Version 3:
```yaml
_change_case.paramCase:
on: example string
```
###### Version 4:
```yaml
_change_case.kebabCase:
on: example string
```
###### Version 3:
```yaml
_change_case.headerCase:
on: example string
```
###### Version 4:
```yaml
_change_case.trainCase:
on: example string
```
111 changes: 56 additions & 55 deletions packages/docs/operators/_change_case.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ _ref:
The `_change_case` operator takes `on` and `options` as arguments.
The `on` argument is the input to be transformed and can be a `string`, `array` or `object`.
The `options` argument is an object with the following properties:
- `splitRegexp`: The regular expression used to split `on` into word segments. Can be a `string` or an `object` with `pattern` and `flags`.
- `stripRegexp`: The regular expression used to remove extraneous characters (default: `/[^A-Z0-9]/gi`). Can be a `string` or an `object` with `pattern` and `flags`.
- `delimiter`: Value used between words (e.g. " ").
- `convertValues`: Only used when `on` is an object. Toggles whether the object values are converted to the new case (`true`) or left as-is (`false`). Default `true`.
- `convertKeys`: Only used when `on` is an object. Toggles whether the object keys are converted to the new case (`true`) or left as-is (`false`). Default `false`.
- `convertValues`: Only used when `on` is an object. Toggles whether the object values are converted to the new case (`true`) or left as-is (`false`). Default `true`.
- `delimiter`: Value used between words (e.g. " ").
- `locale`: Lower/upper according to specified locale, defaults to host environment. Set to false to disable.
- `mergeAmbiguousCharacters`: By default, pascalCase and snakeCase separate ambiguous characters with _. To merge them instead, set mergeAmbiguousCharacters to true.
- `prefixCharacters``: A string that specifies characters to retain at the beginning of the string. Defaults to "".
- `split`: A function to define how the input is split into words.
- `suffixCharacters``: A string that specifies characters to retain at the end of the string. Defaults to "".
The `on` argument behaves as follows:
###### string
Expand Down Expand Up @@ -87,45 +90,21 @@ _ref:
```
Returns: `{ "foo": "bar" }`
###### String with options.splitRegexp string:
```yaml
_change_case.sentenceCase:
on: 'foo8ar'
options:
splitRegexp: '([a-z])([A-Z0-9])'
```
Returns: `"Foo 8ar"`
###### String with options.stripRegexp string:
```yaml
_change_case.sentenceCase:
on: 'FOO8AR'
options:
stripRegexp: '[^A-Z]'
```
Returns: `"Foo ar"`
###### String with options.splitRegexp object:
###### Object with options.SPLT:
```yaml
_change_case.sentenceCase:
on: 'foo8ar'
options:
splitRegexp:
pattern: '([a-z])([A-Z0-9])'
flags: 'gi'
```
Returns: `"F oo 8a r"`
###### String with options.stripRegexp object:
```yaml
_change_case.sentenceCase:
on: 'foo8ar'
_change_case.capitalCase:
on: this123is_an example string
options:
stripRegexp:
pattern: '[^A-Z]'
flags: gi
split:
_function:
__string.split:
- __string.replace:
- __args: 0
- '123'
- '_'
- '_'
```
Returns: `"Foo ar"`
Returns: `This Is An example string`
methods:
- name: camelCase
Expand All @@ -147,6 +126,7 @@ _ref:
on: 'my_variable'
```
Returns: `"myVariable"`
- name: capitalCase
types: |
```
Expand All @@ -166,6 +146,7 @@ _ref:
on: 'my_variable'
```
Returns: `"My Variable"`
- name: constantCase
types: |
```
Expand Down Expand Up @@ -206,7 +187,7 @@ _ref:
```
Returns: `"my.variable"`
- name: headerCase
- name: kebabCase
types: |
```
({on: string, options: object}): string
Expand All @@ -217,14 +198,14 @@ _ref:
([on: array, options: object]): array
```
description: |
The `_change_case.headerCase` method transforms `on` into a dash separated string of capitalized words.
The `_change_case.kebabCase` method transforms `on` into a lower cased string with dashes between words.
examples: |
###### Transform snake_case variable to headerCase:
###### Transform snake_case variable to kebabCase:
```yaml
_change_case.headerCase:
_change_case.kebabCase:
on: 'my_variable'
```
Returns: `"My-Variable"`
Returns: `"my-variable"`
- name: noCase
types: |
Expand All @@ -246,7 +227,7 @@ _ref:
```
Returns: `"my variable"`
- name: paramCase
- name: pascalCase
types: |
```
({on: string, options: object}): string
Expand All @@ -257,16 +238,16 @@ _ref:
([on: array, options: object]): array
```
description: |
The `_change_case.paramCase` method transforms `on` into a lower cased string with dashes between words.
The `_change_case.pascalCase` method transforms `on` into a string of capitalized words without separators.
examples: |
###### Transform snake_case variable to paramCase:
###### Transform snake_case variable to pascalCase:
```yaml
_change_case.paramCase:
_change_case.pascalCase:
on: 'my_variable'
```
Returns: `"my-variable"`
Returns: `"MyVariable"`
- name: pascalCase
- name: pascalSnakeCase
types: |
```
({on: string, options: object}): string
Expand All @@ -277,14 +258,14 @@ _ref:
([on: array, options: object]): array
```
description: |
The `_change_case.pascalCase` method transforms `on` into a string of capitalized words without separators.
The `_change_case.pascalSnakeCase` method transforms `on` into a string of capitalized words with underscores between words.
examples: |
###### Transform snake_case variable to pascalCase:
###### Transform snake_case variable to pascalSnakeCase:
```yaml
_change_case.pascalCase:
on: 'my_variable'
```
Returns: `"MyVariable"`
Returns: `"My_Variable"`
- name: pathCase
types: |
Expand All @@ -304,7 +285,7 @@ _ref:
_change_case.pathCase:
on: 'my_variable'
```
Returns:
Returns: `my/variable`
- name: sentenceCase
types: |
Expand Down Expand Up @@ -345,3 +326,23 @@ _ref:
on: 'My variable'
```
Returns: `"my_variable"`
- name: trainCase
types: |
```
({on: string, options: object}): string
([on: string, options: object]): string
({on: object, options: object}): object
([on: object, options: object]): object
({on: array, options: object}): array
([on: array, options: object]): array
```
description: |
The `_change_case.trainCase` method transforms `on` into a dash separated string of capitalized words.
examples: |
###### Transform snake_case variable to trainCase:
```yaml
_change_case.trainCase:
on: 'my_variable'
```
Returns: `"My-Variable"`
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"dependencies": {
"@lowdefy/helpers": "4.0.0-rc.15",
"@lowdefy/operators": "4.0.0-rc.15",
"change-case": "4.1.2"
"change-case": "5.4.0"
},
"devDependencies": {
"@swc/cli": "0.1.63",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import {
capitalCase,
constantCase,
dotCase,
headerCase,
kebabCase,
noCase,
paramCase,
pascalCase,
pascalSnakeCase,
pathCase,
sentenceCase,
snakeCase,
trainCase,
} from 'change-case';
import { get, type } from '@lowdefy/helpers';
import { runClass } from '@lowdefy/operators';
Expand All @@ -35,31 +36,14 @@ const changeCase = {
capitalCase,
constantCase,
dotCase,
headerCase,
kebabCase,
noCase,
paramCase,
pascalCase,
pascalSnakeCase,
pathCase,
sentenceCase,
snakeCase,
};

const prepRegex = (prop, location) => {
const regex = type.isString(prop) ? { pattern: prop } : prop;
if (!type.isObject(regex)) {
throw new Error(
`Operator Error: regex must be string or an object. Received ${JSON.stringify(
prop
)} at ${location}.`
);
}
try {
return new RegExp(regex.pattern, regex.flags ?? 'gm');
} catch (e) {
throw new Error(
`Operator Error: ${e.message}. Received: ${JSON.stringify(prop)} at ${location}.`
);
}
trainCase,
};

const prep = (args, { location }) => {
Expand All @@ -71,14 +55,6 @@ const prep = (args, { location }) => {
)} at ${location}.`
);
}
if (type.isObject(options)) {
if (options.splitRegexp) {
options.splitRegexp = prepRegex(options.splitRegexp, location);
}
if (options.stripRegexp) {
options.stripRegexp = prepRegex(options.stripRegexp, location);
}
}
return args;
};

Expand Down

0 comments on commit 5cfe04a

Please sign in to comment.