Skip to content

Commit

Permalink
Chore/formatting setup (#735)
Browse files Browse the repository at this point in the history
* chore: add eslint, prettier and lint staged configs

* chore: run eslint:fix

* chore: run prettier
  • Loading branch information
chmelevskij authored Jul 15, 2024
1 parent 9b9926e commit a01caf5
Show file tree
Hide file tree
Showing 53 changed files with 28,860 additions and 24,490 deletions.
8 changes: 8 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.json
*.html
*.less
*.css
package.json
docusaurus.config.js
public/
src/vendor/
40 changes: 40 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module.exports = {
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
},
extends: ['eslint:recommended', 'prettier'],
root: true,
env: {
node: true,
jquery: true,
es2017: true,
browser: true,
webextensions: true,
},
rules: {
// TODO: currently a lot of these issues are marked as
// warnings because they are in the codebase already
// and I don't want to fix them all at once.
// Eventually, they should be fixed and the rules
// should be set to 'error' (default in preset).
'no-var': 'warn',
'no-unused-vars': 'warn',
'no-undef': 'warn',
'no-redeclare': 'warn',
'no-prototype-builtins': 'warn',
'no-empty': 'warn',
'no-inner-declarations': 'warn',
'no-fallthrough': 'warn',
'no-useless-escape': 'warn',
'no-constant-condition': 'warn',
'no-unreachable': 'warn',
'no-duplicate-case': 'warn',
'no-dupe-keys': 'warn',
'no-irregular-whitespace': 'warn',
'no-case-declarations': 'warn',
'prefer-template': 'warn',
'comma-dangle': ['warn', 'always-multiline'],
semi: ['error', 'always'],
},
};
7 changes: 7 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"**/*.{tsx,js}": [
"prettier --write --ignore-unknown",
"eslint --fix --ext .tsx,.js"
],
"**/*.{html,md,mdx,less,css,json}": ["prettier --write --ignore-unknown"]
}
36 changes: 36 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# node.js npm related

node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Configurator Build process
cache/
apps/
dist/
public/
src/vendor/
dist_cordova/
debug/
release/
testresults/
.eslintcache
cordova/bundle.keystore

# OSX
.DS_store

# artefacts for Visual Studio Code
/.vscode/

# NetBeans
nbproject/

# IntelliJ
.idea

# Eclipse
.project
.settings/
test-results-junit/
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"scripts": {
"start": "vite",
"build": "vite build",
"preview": "vite preview"
"preview": "vite preview",
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"format": "prettier --write src"
},
"repository": {
"type": "git",
Expand All @@ -20,12 +23,15 @@
"dependencies": {
"Leaflet.MultiOptionsPolyline": "hgoebl/Leaflet.MultiOptionsPolyline",
"bootstrap": "~3.4.1",
"eslint": "^8.24.0",
"eslint-config-prettier": "^8.5.0",
"html2canvas": "^1.0.0-rc.5",
"jquery": "^3.7.1",
"jquery-ui": "^1.13.2",
"leaflet": "^1.9.3",
"leaflet-marker-rotation": "^0.4.0",
"lodash": "^4.17.21",
"prettier": "^2.8.1",
"throttle-debounce": "^5.0.0",
"vite": "^5.2.6",
"vite-plugin-pwa": "^0.19.7"
Expand Down
159 changes: 78 additions & 81 deletions src/cache.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,88 @@
/**
* A FIFO cache to hold key-pair mappings. Its capacity will be at least the initialCapacity
* supplied on creation, which you can increase by increasing the "capacity" property.
*
* supplied on creation, which you can increase by increasing the "capacity" property.
*
* One extra element beyond the set capacity will be stored which can be fetched by calling "recycle()".
* This allows the oldest value to be removed in order to be reused, instead of leaving it to be collected
* This allows the oldest value to be removed in order to be reused, instead of leaving it to be collected
* by the garbage collector.
*
*
* Element age is determined by the time it was added or last get()'d from the cache.
*/
export function FIFOCache(initialCapacity) {
//Private:
var
queue = [],
items = {};
//Private:
let queue = [],
items = {};

function removeFromQueue(key) {
for (var i = 0; i < queue.length; i++) {
if (queue[i] == key) {
//Assume there's only one copy to remove:
for (var j = i; j < queue.length - 1; j++) {
queue[j] = queue[j + 1];
}

queue.length--;
break;
}
function removeFromQueue(key) {
for (let i = 0; i < queue.length; i++) {
if (queue[i] == key) {
//Assume there's only one copy to remove:
for (let j = i; j < queue.length - 1; j++) {
queue[j] = queue[j + 1];
}

queue.length--;
break;
}
}

//Public:
this.capacity = initialCapacity;

/**
* Remove and return the oldest value from the cache to be reused, or null if the cache wasn't full.
*/
this.recycle = function() {
if (queue.length > this.capacity) {
var
key = queue.shift(),
result = items[key];

delete items[key];

return result;
}

return null;
};

/**
* Add a mapping for the given key to the cache. If an existing value with that key was
* present, it will be overwritten.
*/
this.add = function(key, value) {
// Was this already cached? Bump it back up to the end of the queue
if (items[key] !== undefined)
removeFromQueue(key);

queue.push(key);

items[key] = value;

while (queue.length > this.capacity + 1) {
delete items[queue.shift()];
}
};

/**
* Return the value in the cache that corresponds to the given key, or undefined if it has
* expired or had never been stored.
*/
this.get = function(key) {
var item = items[key];

if (item) {
removeFromQueue(key);
queue.push(key);
}

return item;
};

/**
* Erase the entire content of the cache
*/
this.clear = function() {
queue = [];
items = {};
};
}
}

//Public:
this.capacity = initialCapacity;

/**
* Remove and return the oldest value from the cache to be reused, or null if the cache wasn't full.
*/
this.recycle = function () {
if (queue.length > this.capacity) {
let key = queue.shift(),
result = items[key];

delete items[key];

return result;
}

return null;
};

/**
* Add a mapping for the given key to the cache. If an existing value with that key was
* present, it will be overwritten.
*/
this.add = function (key, value) {
// Was this already cached? Bump it back up to the end of the queue
if (items[key] !== undefined) removeFromQueue(key);

queue.push(key);

items[key] = value;

while (queue.length > this.capacity + 1) {
delete items[queue.shift()];
}
};

/**
* Return the value in the cache that corresponds to the given key, or undefined if it has
* expired or had never been stored.
*/
this.get = function (key) {
let item = items[key];

if (item) {
removeFromQueue(key);
queue.push(key);
}

return item;
};

/**
* Erase the entire content of the cache
*/
this.clear = function () {
queue = [];
items = {};
};
}
Loading

0 comments on commit a01caf5

Please sign in to comment.