ngx-common
v1.0.3
Published
Common functions to be used for React, Angular and Node.js
Downloads
2
Readme
NGX COMMON lt;dr Common JS/TS functions
This repository is a set of functions that might be useful during developing JavaScript applications.
Installing
You can install this repository from npm:
npm install ngx-common --save
Map/Geographical functions
Here you can see most of the public available functions and classes.
FindBoundsCenter
Finds a center position inside a map, a rectangular area. Can be useful to put a marker in middle of a map which user can see.
import { FindBoundsCenter } from 'ngx-common/maps';
Example:
import { MapBounds, FindBoundsCenter } from 'ngx-common/maps';
const bounds: MapBounds = {
b: {
b: 100,
f: 500
},
f: {
b: 500,
f: 100
}
};
test('FindBoundsCenter must return a point in center', function () {
const center = FindBoundsCenter(bounds);
expect(center.lat).toBe(300);
expect(center.lng).toBe(300);
});
FindInnerRandomPosition
Finds a random position inside a rectangular area. In case you want to mock some maps, you can use this function to create a randomly marker for map (google map, etc)
Usage:
import { MapBounds, FindInnerRandomPosition } from 'ngx-common/maps';
Please review the package tests, in order to see how it's being tested and worked in action.
Routing and mocking
In many applications, we are working with webservers. It's totally invauable to make mock data before the api being ready. The reason is, many application are being developed by different teams so you are not relaying on other people work to be ready. Besides, your application works without being dependent on another package partially or simulated.
We are giving a set of functions to integrate these kind of implementations easier.
GetMatchRoute
Finds specific route in your routes which is compatible with user requsted url. Let's specify your routes in an array:
const routes: Array<HttpRoute> = [
{
url: '/api/users',
method: 'GET'
},
{
url: '/api/user',
method: 'POST'
},
{
url: '/api/user/:id',
method: 'GET'
}
];
Now, when user asks for `/api/user/313' how can you find out which url is being requsted? User id is different from each request. So if you want to implement this more smoothly and more real, you need to detect this.
const $search: HttpRoute = { url: '/api/user', method: 'POST'};
const $match = GetMatchRoute($search, routes);
expect($match.method).toBe($search.method);
});