Skip to content

Instantly share code, notes, and snippets.

@achepukov
achepukov / Binary-search.js
Created April 6, 2021 00:42
Binary search in javascript
const binSearch = (list, val) => {
let min = 0, max = list.length - 1, index, guess;
while(min <= max) {
index = Math.ceil((min + max) / 2, 10);
guess = list[index];
if (guess === val) {
return index;
} else if (guess > val) {
max = index - 1;
@achepukov
achepukov / apiCall.js
Last active March 18, 2019 10:13
Call api wrapper
// @flow
export class ApiCall {
constructor(url: string) {
this.url = url;
}
get = (id?: string) => app.api.get(id ? `${this.url}/${id}` : this.url);
post = (body: object = {}) => app.api.post(body);
put = (body: object = {}) => app.api.put(body);
delete = (id?: string) => app.api.delete(`${this.url}/${id}`);
<?php
/**
* UniqueFileName
* Check whether file exists in path, and build unique name for them
*/
class UniqueFileName
{
/**
* @var string path to directory where file suppose to be located
@achepukov
achepukov / UploadErrorMessage.php
Created July 2, 2018 06:56
Php upload errror -> string
<?php
/**
* UploadErrorMessage
*/
class UploadErrorMessage
{
/**
* @var int upload error code
*/
@achepukov
achepukov / reactWithHtmlForm.js
Last active November 12, 2018 10:12
React form component using html form
import React from 'react';
export default class extends React.PureComponent {
onSubmit = (event) => {
event.preventDefault();
const { action, method, elements } = event.target;
const data = Array.from(elements).reduce( (result, currentElement) => ({
[currentElement.id]: currentElement.value,
...result
##
## Prefer mongodump/mongorestore instead of mongoexport/mongoimport,
## as it will "export" additional metadata, like indexes etc.
##
### Mongodump example
# Required: host, db, collection, out
# Optional: query (limit results)
mongodump --host mongodb1.example.net \
@achepukov
achepukov / Autoload.php
Last active September 2, 2016 12:40
Simple php autoloader
<?php
/**
* Autoload:
* new A() -> A.php
* new B1\A1\A -> B1/A1/A.php
*/
class Autoload
{
protected $_basePath;
@achepukov
achepukov / index.php
Created June 29, 2016 07:41
Test generator - creates test when app failed with exception
<?php
require_once 'application.php';
require_once 'generator.php';
$app = new Application($_GET, $_POST);
$testGenerator = Generator::factory();
try {
$app->execute();
} catch(Exception $e) {
#!/bin/bash
#
# Original for 5.3 by Ruben Barkow (rubo77) http://www.entikey.z11.de/
# release 1 PHP5.4 to 5.3 by Emil Terziev ( foxy ) Bulgaria
# Originally Posted by Bachstelze http://ubuntuforums.org/showthread.php?p=9080474#post9080474
# OK, here's how to do the Apt magic to get PHP packages from the precise repositories:
echo "Am I root? "
if [ "$(whoami &2>/dev/null)" != "root" ] && [ "$(id -un &2>/dev/null)" != "root" ] ; then