Skip to content

Commit

Permalink
Prettier (#6)
Browse files Browse the repository at this point in the history
* Add deps

* Add prettier.config.js

* Prettify all

* Add “Check prettier” step to CI

* Refactor ymaps.js

* Add .prettierignore
  • Loading branch information
sergeysolovev committed Jun 20, 2019
1 parent b676157 commit 32c9767
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 66 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
- run:
name: Build
command: yarn build
- run:
name: Check prettier
command: yarn prettier-check
- run:
name: Test
command: yarn test
Expand Down
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package.json
node_modules/
dist/
22 changes: 11 additions & 11 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ orientation.
Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
- Using welcoming and inclusive language
- Being respectful of differing viewpoints and experiences
- Gracefully accepting constructive criticism
- Focusing on what is best for the community
- Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
- The use of sexualized language or imagery and unwelcome sexual attention or
advances
- Trolling, insulting/derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
- Other conduct which could reasonably be considered inappropriate in a
professional setting

### Our Responsibilities
Expand Down
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

Yandex Maps on-demand promise-based widget loader.

* Appends a script tag, referencing [Yandex Maps API](https://tech.yandex.com/maps/jsapi/), to document body, when is told to do so.
* Provides a promise, that resolves, when the script is loaded and the [API is ready](https://tech.yandex.com/maps/doc/jsapi/2.1/dg/concepts/load-docpage/#api-ready).
- Appends a script tag, referencing [Yandex Maps API](https://tech.yandex.com/maps/jsapi/), to document body, when is told to do so.
- Provides a promise, that resolves, when the script is loaded and the [API is ready](https://tech.yandex.com/maps/doc/jsapi/2.1/dg/concepts/load-docpage/#api-ready).

## Installation

Expand All @@ -24,19 +24,23 @@ If `ymaps.load` was called without args, default `script src` value would be `"/
```javascript
import ymaps from 'ymaps';

ymaps.load().then(maps => {
const map = new maps.Map('your-map-container', {
center: [-8.369326, 115.166023],
zoom: 7
});
})
.catch(error => console.log('Failed to load Yandex Maps', error));
ymaps
.load()
.then(maps => {
const map = new maps.Map('your-map-container', {
center: [-8.369326, 115.166023],
zoom: 7
});
})
.catch(error => console.log('Failed to load Yandex Maps', error));
```

To use another language simply pass a valid `src` to `ymaps.load`

```javascript
ymaps.load('https://api-maps.yandex.ru/2.1/?lang=en_US').then(maps => { /* ... */ })
ymaps.load('https://api-maps.yandex.ru/2.1/?lang=en_US').then(maps => {
/* ... */
});
```

The information about other options is available in the [API docs](https://tech.yandex.com/maps/doc/jsapi/2.1/dg/concepts/load-docpage/). Please check out [known issues](#known-issues) to see not supported options.
Expand All @@ -53,7 +57,7 @@ yarn test

## Known issues

* `load` parameter, except when it's set to `package.full` is causing an error "TypeError: s is not a constructor". To reproduce, try to pass `"//api-maps.yandex.ru/2.1/?lang=en_US&load=Map"` to `ymaps.load` in the [example above](#usage). PR is welcome!
- `load` parameter, except when it's set to `package.full` is causing an error "TypeError: s is not a constructor". To reproduce, try to pass `"//api-maps.yandex.ru/2.1/?lang=en_US&load=Map"` to `ymaps.load` in the [example above](#usage). PR is welcome!

## Contributing

Expand Down
4 changes: 1 addition & 3 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"typeAcquisition": {
"include": [
"jest"
]
"include": ["jest"]
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"prebuild": "yarn clean",
"build": "rollup -c",
"clean": "del-cli dist",
"prettier": "prettier --write \"**/*.{js,json,md}\"",
"prettier-check": "prettier --check \"**/*.{js,json,md}\"",
"publishOnly": "yarn build",
"test": "jest"
},
Expand All @@ -40,6 +42,7 @@
"del-cli": "2.0.0",
"file-url": "3.0.0",
"jest": "24.8.0",
"prettier": "1.18.2",
"rollup": "1.15.6",
"rollup-plugin-babel": "4.3.2"
}
Expand Down
3 changes: 3 additions & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
singleQuote: true
};
44 changes: 21 additions & 23 deletions src/ymaps.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
export default {
load(src) {
src = src || '//api-maps.yandex.ru/2.1/?lang=en_RU';

load(src = '//api-maps.yandex.ru/2.1/?lang=en_RU') {
const getNsParamValue = () => {
var results = RegExp('[\\?&]ns=([^&#]*)').exec(src);
return results === null ? '' :
decodeURIComponent(results[1].replace(/\+/g, ' '));
const results = src.match(/[\\?&]ns=([^&#]*)/);
return results === null
? ''
: decodeURIComponent(results[1].replace(/\+/g, ' '));
};

this.promise = this.promise || new Promise((resolve, reject) => {
let elem = document.createElement('script');
elem.type = 'text/javascript';
elem.src = src;
elem.onload = resolve;
elem.onerror = e => reject(e)
document.body.appendChild(elem);
})
.then(() => {
const ns = getNsParamValue();
if (ns && ns !== 'ymaps') {
(1,eval)(`var ymaps = ${ns};`);
}
return new Promise(resolve => ymaps.ready(resolve));
});

if (!this.promise) {
this.promise = new Promise((resolve, reject) => {
const scriptElement = document.createElement('script');
scriptElement.onload = resolve;
scriptElement.onerror = reject;
scriptElement.type = 'text/javascript';
scriptElement.src = src;
document.body.appendChild(scriptElement);
}).then(() => {
const ns = getNsParamValue();
if (ns && ns !== 'ymaps') {
(1, eval)(`var ymaps = ${ns};`);
}
return new Promise(resolve => ymaps.ready(resolve));
});
}
return this.promise;
}
};
27 changes: 9 additions & 18 deletions test/ymaps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,17 @@ describe('ymaps', () => {
});

describe('ymaps.load', () => {
const src = fileUrl(
path.resolve(__dirname, 'ymaps.vendor.mock.js')
);
const src = fileUrl(path.resolve(__dirname, 'ymaps.vendor.mock.js'));

beforeEach(() => {
ymaps.promise = null;
document.querySelectorAll('script').forEach(script => script.remove());
});

it('returns promise, that resolves to vendor ymaps object', () => {
return expect(ymaps.load(src))
.resolves
.toMatchObject({
ready: expect.any(Function)
});
return expect(ymaps.load(src)).resolves.toMatchObject({
ready: expect.any(Function)
});
});

it(`appends script element to body
Expand All @@ -38,19 +34,14 @@ describe('ymaps', () => {
});

it(`doesn't append the script twice`, () => {
return Promise
.all([
ymaps.load(src),
ymaps.load(src)
])
.then(() => {
expect(document.querySelectorAll('script')).toHaveLength(1);
});
return Promise.all([ymaps.load(src), ymaps.load(src)]).then(() => {
expect(document.querySelectorAll('script')).toHaveLength(1);
});
});

it(`rejects if the script src is invalid`, () => {
console.error = jest.fn();
return expect(ymaps.load('wrong_url')).rejects.toBeDefined();
});
})
})
});
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3456,6 +3456,11 @@ prelude-ls@~1.1.2:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=

prettier@1.18.2:
version "1.18.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea"
integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==

pretty-format@^24.8.0:
version "24.8.0"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-24.8.0.tgz#8dae7044f58db7cb8be245383b565a963e3c27f2"
Expand Down

0 comments on commit 32c9767

Please sign in to comment.