Skip to content

Commit

Permalink
ref: creplace combineURLs by concatURLs
Browse files Browse the repository at this point in the history
  • Loading branch information
saeidalidadi committed Oct 25, 2023
1 parent 39c5861 commit 97022f6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ yarn add onstage-js-utils

All string manipulations

- `combineURLs`
This function will concat your base URL to any path you want without any fear of double slashes `//`
- `concatURLs`
This function will concatenate your base URL to any path you want without any fear of double slashes `//`

```javascript
import { combineURLs } from "onstage-js-utils";
import { concatURLs } from "onstage-js-utils";

combineURLs("https://example.com/", "/users") // output: https://example.com/users
combineURLs("https://example.com", "users") // output: https://example.com/users
combineURLs("https://example.com", "//users") // output: https://example.com/users
concatURLs("https://example.com/", "/users") // output: https://example.com/users
concatURLs("https://example.com", "users") // output: https://example.com/users
concatURLs("https://example.com", "//users") // output: https://example.com/users
```
- `combineURLs`
This function is deprecated version of `concatURLs`

- `withUnderscore()`
Replaces strings seperator with `_`.
Expand Down Expand Up @@ -130,9 +132,9 @@ objectsDiff(changed, base)
- `fetchJsonRes(res: Response)`

```javascript
import {fetchJsonRes, combineURLs} from "onstage-js-utilities";
import {fetchJsonRes, concatURLs} from "onstage-js-utilities";

fetch(combineURLs(HOST, "users"))
fetch(concatURLs(HOST, "users"))
.then(fetchJsonRes)
.then(json => {
// json data
Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import _isObject from "lodash.isobject";
* @param {string} baseURL - like "http://example.com/"
* @param {string} relativeURL - like "/users"
* @returns {string} url - combined URL
* @deprecated
*/

export const combineURLs = (baseURL: string, relativeURL: string): string => {
Expand All @@ -16,6 +17,14 @@ export const combineURLs = (baseURL: string, relativeURL: string): string => {
return url;
};

/**
* An alias for combineURLs
*
*/
export const concatURLs = (...args: [string, string]) => {
return combineURLs.apply(null, args);
};

/**
* Replace other chars with `_`
* @param {string} str with not letter chars
Expand Down
12 changes: 12 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
rangeLetters,
withUnderscore,
toSnakeCase,
concatURLs,
} from "../src";
import fetchMock from "jest-fetch-mock";

Expand All @@ -30,6 +31,17 @@ describe("Strings", () => {
expect(result).toEqual("http://test.pass/this/test");
});
});

// This method is an alias for combineURLs
// also will support www url addresses with protocols
describe("concatURLs", () => {
test("Should concat two parts when second part starts with slash", () => {
const partOne = "http://one.me/";
const partTwo = "/two/me";
const result = concatURLs(partOne, partTwo);
expect(result).toBe("http://one.me/two/me");
});
});
describe("Namings", () => {
test("Should split string with `_`.", () => {
let result = withUnderscore("onstage-js-utils");
Expand Down

0 comments on commit 97022f6

Please sign in to comment.