Javascript thinks you should know to make sure you understand nodejs properly
🎯
- Youtube @flashcodes (Igor Berlenko)
- @berliangor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var crypto = require('crypto'); | |
var secret = crypto.randomBytes(24); | |
function encrypt(plaintext) { | |
var cipher = crypto.createCipher('aes-128-cbc', secret); | |
cipher.setAutoPadding(false); | |
var ciphertext = ''; | |
for (var i=0; i < plaintext.length; i+=16) { | |
ciphertext += cipher.update(plaintext.substr(i, i+16), 'utf8', 'base64'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$file = file_get_contents('aes.txt'); | |
$data = json_decode($file); | |
$orig = $data->data; | |
$key = pack('H*', "3dbb8dd8a8b153e9f1aaab6d657e01c2"); | |
$result = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, '3dbb8dd8a8b153e9f1aaab6d657e01c2', $orig, MCRYPT_MODE_CBC, base64_decode($data->iv)); | |
echo $result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* sequence() { | |
let i = 0; | |
while(++i) { | |
yield i; | |
} | |
} | |
for(let item of sequence()) { | |
console.log(item); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var co = require('co'); | |
function getResult(item) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve(item * 2); | |
}, 100); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var co = require('co'); | |
let items = [1,2,3]; | |
function* getResult(item) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => resolve(item * 2), 100); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var co = require('co'); | |
function getResult(item) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
if(item > 5) { | |
reject('Break! Last page reached.'); | |
} else { | |
resolve(item * 2); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
var cheerio = require('cheerio'); | |
var co = require('co'); | |
function parse(url) { | |
// Make promise from calback | |
return new Promise((resolve, reject) => { | |
request({url}, function(error, response, body) { | |
if(error) return reject(error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NodeJS | |
const assert = require('assert'); | |
/** | |
* Flatten an array of arbitrarily nested arrays of integers into a flat array of integers. | |
* e.g. [[1,2,[3]],4] -> [1,2,3,4]. | |
* | |
* @name flatten | |
* @param {Array} arr An array to flatten, nested or otherwise | |
* @returns {Array} the flattened array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.24; | |
interface IToken { | |
function name() external view returns(string); | |
function symbol() external view returns(string); | |
function decimals() external view returns(uint8); | |
function totalSupply() external view returns (uint256); |
OlderNewer