101 هي مكتبة أدوات مساعدة في JavaScript للتعامل مع البيانات غير القابلة للتغيير (Immutable Data) بأسلوب البرمجة الوظيفية.
طريقة الاستخدام (Usage)
const isObject = require('101/isObject')
isObject({}) // → true
يتم تصدير كل دالة كحزمة منفصلة لتقليل حجم الحزمة الإجمالية إلى الحد الأدنى.
فحص الأنواع (Type Checking)
isObject({})
isString('str')
isRegExp(/regexp/)
isBoolean(true)
isEmpty({})
isFunction(x => x)
isInteger(10)
isNumber(10.1)
instanceOf(obj, 'string')
الكائنات (Objects)
أمثلة
let obj = {}
التحديث (Update)
obj = put(obj, 'user.name', 'John')
// → { user: { name: 'John' } }
القراءة (Read)
pluck(name, 'user.name')
// → 'John'
الحذف (Delete)
obj = del(obj, 'user')
// → { }
دوال الوصول (Accessors)
pluck(state, 'user.profile.name')
pick(state, ['user', 'ui'])
pick(state, /^_/)
تستخرج دالة pluck قيمة متداخلة معينة، بينما تعيد دالة pick مجموعة فرعية من الكائن.
دوال التعيين (Setters)
put(state, 'user.profile.name', 'john')
See: put
الحذف (Deletion)
del(state, 'user.profile')
omit(state, ['user', 'data'])
تشبه دالة omit دالة del، لكنها تدعم إزالة عدة مفاتيح في وقت واحد.
التحقق من مسارات المفاتيح (Keypath Verification)
hasKeypaths(state, ['user'])
hasKeypaths(state, { 'user.profile.name': 'john' })
See: hasKeypaths
القيم (Values)
values(state)
الدوال (Functions)
المجمعات والدوال البسيطة (Combinators)
| الدالة | التعبير المكافئ |
|---|---|
and(x, y) |
x && y |
or(x, y) |
x || y |
xor(x, y) |
!(!x && !y) && !(x && y) |
equals(x, y) |
x === y |
exists(x) |
!!x |
not(x) |
!x |
مثالية لخطوط الأنابيب الوظيفية الخالية من النقاط (point-free) والتركيب.
تركيب الدوال (Composition)
compose(f, g) // x => f(g(x))
curry(f) // x => y => f(x, y)
flip(f) // f(x, y) --> f(y, x)
العطف والفصل المنطقي (Logical Conjunction & Disjunction)
passAll(f, g) // x => f(x) && g(x)
passAny(f, g) // x => f(x) || g(x)
التقارب (Converge)
converge(and, [pluck('a'), pluck('b')])(x)
// → and(pluck(x, 'a'), pluck(x, 'b'))
See: converge
المصفوفات (Arrays)
البحث والمسندات (Search & Predicates)
find(list, x => x.y === 2)
findIndex(list, x => ...)
includes(list, 'item')
last(list)
find(list, hasProps('id'))
التجميع والفهرسة (Grouping & Indexing)
groupBy(list, 'id')
indexBy(list, 'id')
أمثلة عملية واقعية
تركيب المسندات (Composing Predicates)
isFloat = passAll(isNumber, compose(isInteger, not))
// n => isNumber(n) && not(isInteger(n))
function doStuff (object, options) { ... }
doStuffForce = curry(flip(doStuff))({ force: true })