const axios = require('axios');
const xml2js = require('xml2js');
const parser = new xml2js.Parser();
const NULL = 0;
const SECOND = 2;
// Ну концепция данной идеи заключается в том, что если появляется новый канал, то мы его просто добавляем во входной массив,
const rssChannels = [
'https://mos.ru/rss',
'https://lenta.ru/rss/news',
];
// Итоговый, агрегационный массив с новостями со всех полученных источников из входного массива rssChannels
const newsFeed = [];
// Подключение к RSS ленте, получаем XML и приобразуем из него в JS объекты, затем добавляем в агрегационный массив newsFeed
const getChannelFeed = url =>
axios.get(url).then(res => {
return parser.parseStringPromise(res.data)
.then(res => newsFeed.push(...res.rss.channel[NULL].item))
.catch(err => console.log('ERROR: Unable to parse received XML'));
})
.catch(err => console.log('ERROR: Unable to establish URL connection'));
// Заполняем выходной массив контентов
const someFoo = rssChannels =>
Promise.all(rssChannels.map(item => getChannelFeed(item)));
someFoo(rssChannels).then(item => console.log(newsFeed));
const fs = require('fs');
const path = require('path');
async function readDirFiles(dir) {
const filePaths = await fs.promises.readdir(dir, {withFileTypes: true});
return await Promise.all(
filePaths
.filter(e => e.isFile())
.map(async (e) => {
const p = path.join(dir, e.name)
return {
path: p,
content: await fs.promises.readFile(p, {encoding: 'utf8'})
};
})
);
}
readDirFiles('.')
.then(files => {
console.log(files);
})
.catch(err =>
console.error(err)
);
function insert(n, arr) {
let i = 0;
while (n > arr[i]) i++;
arr.splice(i, 0, n);
return arr;
}
for(let i = 0; i < input.files.length; i++) {
const r = reader[i] = new FileReader();
r.addEventListener('load', () => {
document.querySelectorAll('.blah3')[i].setAttribute('src', r.result);
});
r.readAsDataURL(input.files[i]);
}
Object.getOwnPropertyNames(foo).forEach(function(prop) {
Object.defineProperty(
bar,
prop,
Object.getOwnPropertyDescriptor(foo, prop)
);
});
const stream = require('stream');
const fs = require('fs');
class PercentWatcher extends stream.Duplex {
constructor(fd) {
super();
this.size = fs.fstatSync(fd).size;
this.handled = 0;
}
_read() {}
_write(chunk, encoding, callback) {
this.push(chunk);
this.handled += chunk.length;
console.log(`\n\n${Math.round(this.handled / this.size * 100)}%\n\n`);
callback();
}
}
const fd = fs.openSync('file.txt', 'r');
fs.createReadStream(null, {fd}).pipe(new PercentWatcher(fd)).pipe(process.stdout);
<h1>Hello World!</h1>
преобразуется в React.createElement("h1", null, "Hello World!")
() => console.log('res')
ну или console.log.bind(console, 'res')
class Calc extends React.Component {
state = {
valueA: 0,
valueB: 0
};
handleChange = ({target: {name, value}}) => {
if (name) this.setState({[name]: value});
};
render() {
return (
<div onChange={this.handleChange}>
<input type='text' name='valueA' value={this.state.valueA}/>
<input type='text' name='valueB' value={this.state.valueB} />
</div>
);
}
}
componentWillReceiveProps(nextProps) {
if (nextProps.complete && !this.props.complete) this.sendRequest();
}
shouldComponentUpdate(nextProps) {
const changed = Object.keys(nextProps).filter(e => nextProps[e] !== this.props[e]);
if (changed.length == 1 && changed[0] == 'complete') return false;
}