Skip to content

Commit

Permalink
Merge pull request #3 from imglab-io/v0.1.1
Browse files Browse the repository at this point in the history
v0.1.1
  • Loading branch information
jfcalvo authored Mar 15, 2022
2 parents 4de86b3 + e9365f0 commit baf5826
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 3 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@ import Imglab from '@imglab/core'
Imglab.url('assets', 'example.jpeg', { width: 500, height: 600 })
```

## Browser
### Browser

If you prefer to use a `script` tag in your code to load `@imglab/core` try to use [UNPKG](https://unpkg.com) or [JSDELIVR](https://www.jsdelivr.com):

```html
<!-- Using UNPKG -->
<script src="https://unpkg.com/@imglab/[email protected]/dist/imglab.umd.js"></script>
<!-- Or using JDELIVR -->
<script src="https://cdn.jsdelivr.net/npm/@imglab/[email protected]/dist/imglab.umd.js"></script>
```

After adding the library you can use it normally:

```javascript
Imglab.url('assets', 'example.jpeg', { width: 500, height: 600 })
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@imglab/core",
"source": "src/index.js",
"version": "0.1.0",
"version": "0.1.1",
"description": "Official imglab Javascript library",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
Expand Down
11 changes: 10 additions & 1 deletion src/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@ export default class Url {
url.protocol = source.scheme()
url.hostname = source.host
url.port = source.port
url.pathname = source.path(normalizedPath)
url.pathname = source.path(Url.#encodePath(normalizedPath))
url.search = Url.#encodeParams(source, normalizedPath, normalizedParams)

return url.toString()
}

static #encodePath(path) {
if (Utils.isWebURL(path)) {
return encodeURIComponent(path)
} else {
return path.split('/').map((pathComponent) =>
encodeURIComponent(pathComponent)
).join('/')
}
}

static #encodeParams(source, path, params) {
if (Object.keys(params).length === 0) {
Expand Down
12 changes: 12 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export default class Utils {
static #NORMALIZE_PATH_SUFFIX_REGEXP = new RegExp(/\/*$/)
static #NORMALIZE_KEY_REGEXP = new RegExp(/([a-z])([A-Z])/g)

static #WEB_URL_PROTOCOLS = ['https:', 'http:']

static normalizePath(path) {
return path.replace(Utils.#NORMALIZE_PATH_PREFIX_REGEXP, '').replace(Utils.#NORMALIZE_PATH_SUFFIX_REGEXP, '')
}
Expand All @@ -14,6 +16,16 @@ export default class Utils {
}, {})
}

static isWebURL(url) {
try {
const parsedURL = new URL(url)

return Utils.#WEB_URL_PROTOCOLS.includes(parsedURL.protocol)
} catch {
return false
}
}

static #normalizeKey(key) {
return key.replace(Utils.#NORMALIZE_KEY_REGEXP, '$1-$2').replace('_', '-').toLowerCase()
}
Expand Down
Loading

0 comments on commit baf5826

Please sign in to comment.