Skip to content

Commit

Permalink
Lint with eslint, test and increase the version.
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammadSawalhy committed Sep 4, 2020
2 parents b0be801 + 2c1ef1b commit ade8b85
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 68 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module.exports = {
"env": {
"browser": true,
"es2021": true
"es2021": true,
"jest": true
},
"extends": "eslint:recommended",
"parserOptions": {
Expand Down
26 changes: 25 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed
### Security

## [0.0.2] - 2020-9-4
### Fixed
- the second trial to fix readme file issue, after asking [a question](https://stackoverflow.com/questions/63733460/readme-is-deformed-in-npmjs-but-appears-in-github) in stackoverflow.



## [0.0.1] - 2020-9-4
### Fixed
- Change README.md from a binary looking file into a file in bytes, there was weird README in [rakam](https://npmjs.com/package/rakam).


## [0.0.0] - 2020-9-4
### Added
- `math2js` to convert math expression from a string into real js function by generating the code then evaluating it.
- `geometry.angles` property in the exported library, with angles you can get:
- the angle between two vectors or lines, in a clockwise direction or the other one. You can also trim or normalize the angle to be between 0 and 360.
- the min angle between two vectors or lines, in either direction
- as well as the max angle

- `geometry.lines`: here you can get
- line equation `ax+by+c=0`, the line info returned as {a:number,b:number,c:number}
- the intersection point between two lines
- projection of a point on a line
- the dist, or the displacement till the line from a given point, the shortest distance to the line, which is the length of the perpendicular line to this line from that point.

- `core`: that contains some useful functions such as `lcm`, `gcd`, `constrain`, `dist`

The First publish, still uncompleted package to be even used.
Binary file modified README.md
Binary file not shown.
Binary file modified docs/parser/math2js.md
Binary file not shown.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rakam",
"version": "0.0.1",
"version": "0.0.2",
"description": "Intense math library. God willing, we won't stop developing it, we will be so happy when you contribute to make this library eligible for all needs and uses.",
"main": "lib/index.js",
"files": [
Expand Down
17 changes: 3 additions & 14 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export function lcm(...values) {
return Math.abs(product) / Math.pow(this.gcd(...values), values.length - 1);
}


// TODO:

// export function newtonMethod(intialGuess, F, F_prime, cs) {
// let x = intialGuess, x_;
// do {
Expand All @@ -86,17 +89,3 @@ export function lcm(...values) {

// }

// export all the exported functions here as default
export default {
dist,
constrain,
snap,
random,
randomInt,
gcd,
gcd2,
lcm,
newtonMethod,
evaluate,
};

26 changes: 15 additions & 11 deletions src/geometry/angles-calculations.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@

import {constrain} from "../core/index.js";
import {deg} from './angles-conversions.js';
import vector from '../vector.js'

/**
* returns angle between 0 and 2*PI (one round)
* returns the smaller angle between two vectors or lines
* @param {vector} p1 instanceof {x: ___, y: ___},
* @param {vector} p2 instanceof {x: ___, y: ___};
* @param {vector} p1 instanceof vector or {x: ___, y: ___},
* @param {vector} p2 instanceof vector or {x: ___, y: ___};
* @param {object} options instanceof '{}' that defines 'type'.
*/
export function minAngle(p1, p2, {type}) {
type = type || "vectors";
if (type === "vectors") {
let s = p1.dot(p2) / (p1.mag * p2.mag);
let a = Math.acos(Core.constrain(s, -1, 1));
let a = Math.acos(constrain(s, -1, 1));

return this.constrainAngle(a);
} else if (type === "lines") {
Expand All @@ -22,8 +26,8 @@ export function minAngle(p1, p2, {type}) {
/**
* returns angle between 0 and 2*PI (one round)
* returns the bigger angle between two vectors or lines
* @param {vector} p1 instanceof ,
* @param {vector} p2 instanceof ;
* @param {vector} p1 instanceof vector or {x: ___, y: ___},
* @param {vector} p2 instanceof vector or {x: ___, y: ___};
* @param {object} options instanceof '{}' that defines 'type'.
*/
export function maxAngle(p1, p2, {type}) {
Expand All @@ -42,18 +46,18 @@ export function maxAngle(p1, p2, {type}) {
* returns the spwan angle beteen 2 vectors or lines
* when the first turns untill reaching the other one
* in counterclockwise or the other direction
* @param {vector} p1 instanceof {x: ___, y: ___},
* @param {vector} p2 instanceof {x: ___, y: ___}
* @param {vector} p1 instanceof vector or {x: ___, y: ___},
* @param {vector} p2 instanceof vector or {x: ___, y: ___};
* @param {object} options instanceof '{}' that defines 'type', 'dir'.
*/
export function angle(p1, p2, {type, dir}) {
type = type || "vectors";
dir = dir || "counterclockwise";

if (type === "vectors") {
var a1 = this.minAngle(p1, new vector(1, 0));
var a1 = this.minAngle(p1, {x: 1, y: 0});
a1 = p1.y >= 0 ? a1 : -a1;
var a2 = this.minAngle(p2, new vector(1, 0));
var a2 = this.minAngle(p2, {x: 1, y: 0});
a2 = p2.y >= 0 ? a2 : -a2;
let a = dir === "counterclockwise" || dir === "+" ? a2 - a1 : a1 - a2;

Expand Down Expand Up @@ -99,7 +103,7 @@ export function constrainAngle(angle, type = 0) {
}

export function snapAngle(a, valuesTOsnapTO) {
let margin = angles.deg(2.5); /// 2.5deg
let margin = deg(2.5); /// 2.5deg
/// sanp to 30 or 210 deg, and so on.
if (!valuesTOsnapTO) {
let snapTo = [Math.PI / 6, Math.PI / 4, Math.PI / 3, Math.PI / 2]; /// four special angles
Expand All @@ -113,7 +117,7 @@ export function snapAngle(a, valuesTOsnapTO) {
valuesTOsnapTO = snapTo;
}
for (let s of valuesTOsnapTO) {
let a1 = angles.minAngle(vector.fromAngle(a), vector.fromAngle(s)); // angles between two vectors not lines.
let a1 = minAngle(vector.fromAngle(a), vector.fromAngle(s)); // angles between two vectors not lines.
if (a1 <= margin) {
return s;
}
Expand Down
53 changes: 23 additions & 30 deletions src/geometry/angles-conversions.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,38 @@


/**
* return an object with degress, seconds and minutes properities
* @param {*} angle ::: in radian form
* @param {Number} angle ::: in degrees as float number
*/
export function degAngle(angle) {
if (Core.isNumeric(angle)) {
let splitted;
angle = (angle * 180) / Math.PI;
let deg, min, sec;
let getTerm = (a, b) => {
a = "0." + a.toString();
a *= b;
splitted = a.toString().split(".");
splitted = [parseInt(splitted[0]), parseInt(splitted[1])];
return [...splitted];
};
export function degForm(angle) {
if (!isNaN(angle)) {
let deg, min = 0, sec = 0, num;

if (Math.round(angle) != angle) {
splitted = angle.toString().split(".");
splitted = [parseInt(splitted[0]), parseInt(splitted[1])];
deg = splitted[0];
min = getTerm(splitted[1], 60);
sec = getTerm(min[1], 60);
if (Math.round(angle) !== angle) {
deg = angle < 0 ? Math.ceil(angle) : Math.floor(angle); // the same as Math.trunc
// get the decimal number only 0.1326548
// then multiply by 60 and trunc
num = (angle - deg) * 60;
min = angle < 0 ? Math.ceil(num) : Math.floor(num);
num = (num - min) * 60;
sec = angle < 0 ? Math.ceil(num) : Math.floor(num);

if (Math.abs(sec[0] - 60) <= 1) {
min[0]++;
sec[0] = 0;
//#region avoid a tiny error resulting in a slitly different angle
if (Math.abs(sec - 60) <= 1) {
min++;
sec = 0;
}
if (min[0] === 60) {
if (min === 60) {
deg += 1 * Math.sign(deg);
min[0] = 0;
}
return { degrees: deg, minutes: min[0], seconds: sec[0] };
//#endregion
}
return { degrees: angle, minutes: 0, seconds: 0 };
} else {
let cAngle = calculateString(angle);
if (Core.isNumeric(cAngle)) {
return this.degAngle(cAngle);
} else throw new Error("your angle value (" + angle + ') is not valid. :"(');

return { deg, min, sec };

}
throw new Error(`can't convert ${angle} into degrees form, please pass a valid number.`);
}

export function stringDegAngle(angle) {
Expand Down
7 changes: 5 additions & 2 deletions src/geometry/lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function distToLine(v, line) {
*/
export function projectionToLine(v, line) {
if (v && line) {
return Lines.lineIntersection(line, Lines.lineEquation(line.angle + Math.PI / 2, v));
return lineIntersection(line, lineEquation(line.angle + Math.PI / 2, v));
}
}

Expand All @@ -41,5 +41,8 @@ export function lineEquation(angle, trans) {
*/
export function lineIntersection(line1, line2) {
let y = -(line1.c / line1.a - line2.c / line2.a) / (line1.b / line1.a - line2.b / line2.a);
return new vector((-line1.b * y - line1.c) / line1.a, y);
return {
x: (-line1.b * y - line1.c) / line1.a,
y
};
}
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import geometry from './geometry/index.js'
import core from './core/index.js';
import * as core from './core/index.js';
import parser from './parser/index.js';
import version from './version.js';

export default {

...core, /// make sre that any new property won't crash with core properties
...core, /// make sure that any new property won't crash with core properties

parser,

geometry,

version
};
4 changes: 3 additions & 1 deletion src/parser/__tests__/math2js.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable-file no-unused-vars */

// we are using rollup-jest transformer
import parser from '../index.js';
import {defaultHandlers} from '../math2js.js';
Expand Down Expand Up @@ -96,7 +98,7 @@ describe("math2js", ()=>{
return node.check({ type: 'function', name: 'sum' });
},

handle(parserTree, options){
handle(){
// options that has been passed to math2js -> generateJs function
// but options.parserTree = node that has passed this.test not the top most Node
return 'newSum()'
Expand Down
2 changes: 1 addition & 1 deletion src/parser/utils/math2js/generateJs.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function getScopedId(id, { params, scope, undef, header }){
else if (scope instanceof Array) {
let found = false;
for (let i = 0; i < scope.length; i++) {
if (typeof scope[i] === 'object' && scope[i].hasOwnProperty(id)) {
if (typeof scope[i] === 'object' && Object.prototype.hasOwnProperty.call(scope[i], id)) {
found = true;
} else if (typeof scope[i] === 'function'){
if (scope[i](id) !== undefined) {
Expand Down
2 changes: 2 additions & 0 deletions src/parser/utils/math2js/prepareHandlers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable-file no-unused-vars */
import addToHeader from "./addToHeader.js";
import randomName from '../../../utils/randomName.js';
import generateJs from './generateJs.js';
Expand Down Expand Up @@ -99,6 +100,7 @@ export default function prepareHandlers(handlers){
return node.check({ type: 'function', name: 'derive' });
},

// eslint-disable-next-line no-unused-vars
handle(parserTree, {params, scope, handlers, undef, header}) {
// on the test process, we saved the differentiation process data
// TODO:
Expand Down
2 changes: 1 addition & 1 deletion src/version.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

// this file is auto generated
// the current version is:
module.exports = "0.0.4";
export default "0.0.2";
3 changes: 2 additions & 1 deletion utils/generate-version-file.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-undef */
const pkg = require('../package.json')
const filePath = '../src/version.js'
const version = pkg.version
Expand All @@ -7,7 +8,7 @@ const fs = require('fs')
const code = `
// this file is auto generated
// the current version is:
module.exports = "${version}";
export default "${version}";
`

fs.writeFileSync(path.resolve(__dirname, filePath), code)

0 comments on commit ade8b85

Please sign in to comment.