Skip to content

Instantly share code, notes, and snippets.

@hebus
hebus / generic_keyvalue_type.ts
Created September 16, 2021 10:10
generic keyvalue type
// Generic type for: {[key: string]: Type<any>}
type kv<K extends PropertyKey, V = unknown> = {
[key in K]: V;
};
export declare interface Type<T> extends Function {
new (...args: any[]): T;
}
class ClassA{
@hebus
hebus / test.js
Created June 11, 2019 12:59
insérer un elément entre chaque élément du table, lui donner l'index de l'élement suivant et incrémenter l'élement suivant etc.
const i = [0,1,2,3,4];
const r = i.map((v) => [v * 2 , (v*2)+1]);
console.log([].concat(...r));
// output: [0,1,2,3,4,5,6,7,8,9]
@hebus
hebus / Map.js
Created April 1, 2019 08:51
Map() explorations
var s = new Map();
var o = {id:1, name:"test"}
var o1 = {...o, id:2}
var o2 = o;
var arr = [o,o1,o2];
arr.forEach(element => {
s.set(element.id, element);
});
@hebus
hebus / uniqBy.js
Created September 13, 2018 09:36
Group by array transformation
var arr = [{ site_id: 1, col:[] }, { site_id: 1 }, { site_id: 1 }, { site_id: 2 }, { site_id: 1 }, { site_id: 3 }];
const obj = arr.reduce((acc, item) => {
acc[item.site_id] = item;
return acc;
}, {});
console.log(Object.values(obj));
@hebus
hebus / bubblesort.cs
Last active June 5, 2018 15:56
Bubble Sort non optimisé
static void BubbleSort()
{
int[] nums = new int[] { 5, 1, 7, 6, 4, 2, 8, 3 };
for (int i = nums.Length; i > 0; i--)
{
for (int j = 0; j < i - 1; j++)
{
if (nums[j + 1] < nums[j])
{
int t = nums[j];
@hebus
hebus / pgcd.cs
Last active June 5, 2018 15:56
PGCD methode euclide
static int Pgcd(int a, int b)
{
//return euclide(a, b);
return egyptienne(a, b);
}
static int euclide(int a, int b)
{
if (a > b)
{
@hebus
hebus / armstrong.cs
Created May 24, 2018 12:45
Nombre de Armstrong
static List<double> NombresDeArmstrong()
{
// uniquement les nombres de 100 à 999
List<double> results = new List<double>();
int n = 99;
double somme = 0;
string s;
for (int i = 0; i < 1000; i++)
{
n++;
@hebus
hebus / index.js
Created May 7, 2018 07:35
karma config + webpack + coverage
// require all `test/**/*.js`
const testsContext = require.context('./', true, /.+spec.js\b/);
testsContext.keys().forEach(testsContext);
@hebus
hebus / palindrome.js
Last active June 5, 2018 15:58
Is a phrase a palindrome ?
// time complexity : O(n)
function isTextPalindrome(text){
if(text === undefined) return false;
let left = 0;
let right = text.length - 1;
while(left < right){
if(text[left++] !== text[right--]){
return false;
}
@hebus
hebus / spread.js
Created May 31, 2017 12:00
Spread operator
let f = {folder:{id:1, name:'description'}};
let o:{id?:number, name?:string} = {};
(o) = (f && f['folder'])?{...f['folder']}:{};
console.log(o);