Skip to content

Commit 899559f

Browse files
drinckeszongweil
authored andcommitted
Use Math.pow instead of ** (#342)
* enable comments for js * correct the comment file path * remove exponent operators * update version, refresh minified library
1 parent b20f21b commit 899559f

File tree

7 files changed

+84
-32
lines changed

7 files changed

+84
-32
lines changed

.gitignore

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
/bazel-*
66
# Ignore outputs generated during Bazel bootstrapping.
77
/output/
8-
# Ignore Compiled files
8+
# Ignore compiled files
99
**/target/
1010
# Ignore intelliJ auto generated dir
1111
**/.idea/
12-
#Ignore iml extensions files
12+
# Ignore iml extensions files
1313
**/*.iml
14-
#ignore visusl studio auto generated dir
15-
**/.vscode/
14+
# Ignore visual studio auto generated dir
15+
**/.vscode/
16+
# Ignore JS NPM modules
17+
/js/node_modules/
18+
/js/package-lock.json
19+
# Ignore dynamically generated test JSON files.
20+
/js/test/*json
21+
# Ignore Rust lockfile.
22+
/rust/Cargo.lock

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ matrix:
8282
env: OLC_PATH=js
8383
script:
8484
- cd js
85-
- sh test/run_tests.sh
85+
- bash checks.sh
8686

8787
# Python implementation. Lives in python/, tested with bazel.
8888
- language: python

js/checks.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
# Run lint checks on files in the Javascript directory.
3+
# When running within TravisCI, post comments back to the pull request.
4+
# Also converts the test CSV files to JSON ready for the tests to execute.
5+
# Note: must run within the JS directory.
6+
if [ `basename "$PWD"` != "js" ]; then
7+
echo "$0: must be run from within the js directory!"
8+
exit 1
9+
fi
10+
11+
# Require that the NPM install and CSV conversion commands succeed.
12+
set -e
13+
14+
# Install all the dependencies.
15+
npm install
16+
17+
# Convert the CSV test files to JSON and put them in the test directory for serving.
18+
go run ../test_data/csv_to_json.go --csv ../test_data/decoding.csv >test/decoding.json
19+
go run ../test_data/csv_to_json.go --csv ../test_data/encoding.csv >test/encoding.json
20+
go run ../test_data/csv_to_json.go --csv ../test_data/shortCodeTests.csv >test/shortCodeTests.json
21+
go run ../test_data/csv_to_json.go --csv ../test_data/validityTests.csv >test/validityTests.json
22+
23+
set +e
24+
25+
# Run the tests
26+
npm test
27+
# Save the return value for the end.
28+
RETURN=$?
29+
30+
# Run eslint based on local installs as well as in PATH.
31+
# eslint errors will cause a build failure.
32+
ESLINT=eslint
33+
$ESLINT --version >/dev/null 2>&1
34+
if [ $? -ne 0 ]; then
35+
ESLINT=./node_modules/.bin/eslint
36+
fi
37+
38+
$ESLINT --version >/dev/null 2>&1
39+
if [ $? -ne 0 ]; then
40+
echo "\e[1;31mCannot find eslint, check your installation\e[0m"
41+
else
42+
# Run eslint on the source file.
43+
FILE=src/openlocationcode.js
44+
LINT=`$ESLINT $FILE`
45+
if [ $? -ne 0 ]; then
46+
echo -e "\e[1;31mFile has formatting errors:\e[0m"
47+
echo "$LINT"
48+
RETURN=1
49+
if [ -v TRAVIS ]; then
50+
# On TravisCI, send a comment with the diff to the pull request.
51+
go run ../travis-utils/github_comments.go \
52+
--comment '**File has `eslint` errors that must be fixed**:'"<br><pre>$LINT</pre>" \
53+
--file "js/$FILE" \
54+
--pr "$TRAVIS_PULL_REQUEST" \
55+
--commit "$TRAVIS_PULL_REQUEST_SHA"
56+
fi
57+
fi
58+
fi
59+
exit $RETURN

js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "open-location-code",
33
"description": "Library to convert between lat/lng and OLC codes",
4-
"version": "20181203.0.0",
4+
"version": "20190515.0.0",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/google/open-location-code.git"

js/src/openlocationcode.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,11 @@
117117
var PAIR_CODE_LENGTH_ = 10;
118118

119119
// First place value of the pairs (if the last pair value is 1).
120-
var PAIR_FIRST_PLACE_VALUE_ = ENCODING_BASE_**(PAIR_CODE_LENGTH_ / 2 - 1);
120+
var PAIR_FIRST_PLACE_VALUE_ = Math.pow(
121+
ENCODING_BASE_, (PAIR_CODE_LENGTH_ / 2 - 1));
121122

122123
// Inverse of the precision of the pair section of the code.
123-
var PAIR_PRECISION_ = ENCODING_BASE_**3;
124+
var PAIR_PRECISION_ = Math.pow(ENCODING_BASE_, 3);
124125

125126
// The resolution values in degrees for each position in the lat/lng pair
126127
// encoding. These give the place value of each position, and therefore the
@@ -137,20 +138,22 @@
137138
var GRID_ROWS_ = 5;
138139

139140
// First place value of the latitude grid (if the last place is 1).
140-
var GRID_LAT_FIRST_PLACE_VALUE_ = GRID_ROWS_**(GRID_CODE_LENGTH_ - 1);
141+
var GRID_LAT_FIRST_PLACE_VALUE_ = Math.pow(
142+
GRID_ROWS_, (GRID_CODE_LENGTH_ - 1));
141143

142144
// First place value of the longitude grid (if the last place is 1).
143-
var GRID_LNG_FIRST_PLACE_VALUE_ = GRID_COLUMNS_**(GRID_CODE_LENGTH_ - 1);
145+
var GRID_LNG_FIRST_PLACE_VALUE_ = Math.pow(
146+
GRID_COLUMNS_, (GRID_CODE_LENGTH_ - 1));
144147

145148
// Multiply latitude by this much to make it a multiple of the finest
146149
// precision.
147150
var FINAL_LAT_PRECISION_ = PAIR_PRECISION_ *
148-
GRID_ROWS_**(MAX_DIGIT_COUNT_ - PAIR_CODE_LENGTH_);
151+
Math.pow(GRID_ROWS_, (MAX_DIGIT_COUNT_ - PAIR_CODE_LENGTH_));
149152

150153
// Multiply longitude by this much to make it a multiple of the finest
151154
// precision.
152155
var FINAL_LNG_PRECISION_ = PAIR_PRECISION_ *
153-
GRID_COLUMNS_**(MAX_DIGIT_COUNT_ - PAIR_CODE_LENGTH_);
156+
Math.pow(GRID_COLUMNS_, (MAX_DIGIT_COUNT_ - PAIR_CODE_LENGTH_));
154157

155158
// Minimum length of a code that can be shortened.
156159
var MIN_TRIMMABLE_CODE_LEN_ = 6;
@@ -353,8 +356,8 @@
353356
lngVal = Math.floor(lngVal / GRID_COLUMNS_);
354357
}
355358
} else {
356-
latVal = Math.floor(latVal / GRID_ROWS_**GRID_CODE_LENGTH_);
357-
lngVal = Math.floor(lngVal / GRID_COLUMNS_**GRID_CODE_LENGTH_);
359+
latVal = Math.floor(latVal / Math.pow(GRID_ROWS_, GRID_CODE_LENGTH_));
360+
lngVal = Math.floor(lngVal / Math.pow(GRID_COLUMNS_, GRID_CODE_LENGTH_));
358361
}
359362
// Compute the pair section of the code.
360363
for (var i = 0; i < PAIR_CODE_LENGTH_ / 2; i++) {

js/src/openlocationcode.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/test/run_tests.sh

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)