Skip to content

Instantly share code, notes, and snippets.

@NguyenTungs
Last active March 6, 2019 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NguyenTungs/8cbb6bc77060885306cc30f64c89ad06 to your computer and use it in GitHub Desktop.
Save NguyenTungs/8cbb6bc77060885306cc30f64c89ad06 to your computer and use it in GitHub Desktop.
Hẳn là trong mỗi lập trình viên Javascipt đều có cho mình một lib về các func hữu ích, và nó sẽ đi theo mình suốt một dự án hay dài hơn nữa là cả cuộc đời thăng trầm.
Và đây tôi sẽ chia sẻ cho các bạn lib của tôi, lúc đầu tôi làm es5 nhưng sau này tôi đã chuyển về es6 để thuận tiện phù hợp cho các dự án tiếp theo.
Các bạn nào chưa hiểu về cấu trúc của es5 khác với es6 như thế nào thì tôi khuyên hãy đọc qua bài này.
Sự khác biệt cấu trúc ES5 và ES6...
https://anonystick.com/blog-developer/hoc-javascript-su-khac-biet-cau-truc-class-giua-es5-va-es6-sTuezjDk.jsx
Ở phần 1 này tôi giới thiệu cho các bạn một số hàm hay luôn gặp phải trong quá trình xử lý về Array(), hay Object().
Nào bắt đầu:
'use strict'
class Utils {
constructor(){
}
// convert nhieu array tro thanh mot array
combineArray(...arrays){
return [].concat(...arrays)
}
//Loại bỏ những giá trị khong phu hop trong array
compactArray(arr){
return arr.filter(Boolean);
}
//Tim item trong array return true, false
containsArray(arr, value){
return Array.prototype.includes ? arr.includes(value) : arr.some(el => el === value)
}
// tim nhung item khong co trong array khac
differenceArray(arr, ...others){
let combined = this.combineArray(...others) // su dung lai func combine
return arr.filter(el => !combined.some(exclude => el === exclude))
}
// add nhieu object thanh mot object moi
mergeObject (...objects) {
const extend = Object.assign ? Object.assign : (target, ...sources) => {
sources.forEach(source =>
Object.keys(source).forEach(prop => target[prop] = source[prop])
)
return target;
}
return extend({}, ...objects);
}
//return mot array bao gom nhung giatri, tu mot object
getValuesObject(obj){
return Object.keys(obj).map(key => obj[key]);
}
}
Cách sử dụng chúng như thế nào? Đơn giản thôi
const utils = new Utils();
// combineArray(arrays)
// Sử dụng như concat trong es5, convert nhieu array tro thanh mot array
console.log(utils.combineArray([123], [1234], ['a'], ['b'])) // return [123, 1234, "a", "b"]
// compactArray(arrays)
// Loại bỏ những giá trị khong phu hop trong array
console.log(utils.compactArray(['', 0, 3, false, 6, 8, -1])) // return [3, 6, 8, -1]
//containsArray(arrays)
// Tim item trong array return true, false
console.log(utils.containsArray(['1', 3, false, 6, 8, -1, 'anony'], 'anony')) // return true
//differenceArray(arrays)
// tim nhung item khong co trong array khac
console.log(utils.differenceArray([1,2,4,6], [1,2,8], [4,9]))// return 6
//mergeObject(objects)
// add nhieu object thanh mot object moi
console.log(utils.mergeObject({ type: "blog" }, { name: "anonystick.com" })) //return {type: "blog", name: "anonystick.com"}
//getValuesObject(object)
//return mot array bao gom nhung giatri, tu mot object
console.log(utils.getValuesObject({type: 'blog', name: 'anonystick.com'})) // return ["blog", "anonystick.com"]
//Trong phần 1 này tôi đã liệt kê ra những func hữu ích trong Javascript.
//Mời các bạn đón đọc phần 2 tiếp theo.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment