Commits

Lynn Rees  committed 3978bb9

- enhance mapping

  • Participants
  • Parent commits da00a88

Comments (0)

Files changed (3)

File knife/_mixins.py

 
     @memoize
     def _mapping(
-        self, key, value, ky=keys, it=items, vl=values, sm=starmap,
-        m=map, ci=chain.from_iterable,
+        self, key, value, unchain, mapmerge, ky=keys, it=items, vl=values,
+        sm=starmap, m=map, ci=chain.from_iterable,
     ):
-        call = self._identity
+        if mapmerge:
+            self._wrapper = dict
+        if unchain:
+            aget = attrgetter('maps')
+            self._iterable = (aget(i) for i in self._iterable)
         if key:
-            return self._xtend(m(call, ci(m(ky, self._iterable))))
+            return self._xtend(m(self._identity, ci(m(ky, self._iterable))))
         elif value:
-            return self._xtend(m(call, ci(m(vl, self._iterable))))
+            return self._xtend(m(self._identity, ci(m(vl, self._iterable))))
+        call = (lambda x, y: (x, y)) if self._worker is None else self._worker
         return self._xtend(sm(call, ci(m(it, self._iterable))))
 
 

File knife/mixins.py

         with self._chain:
             return self._map()
 
-    def mapping(self, keys=False, values=False):
+    def mapping(self, keys=False, values=False, unchain=False, mapmerge=False):
         '''
         Run :meth:`worker` on incoming :term:`mapping` things.
 
             function in Underscore.php
         '''
         with self._chain:
-            return self._mapping(keys, values)
+            return self._mapping(keys, values, unchain, mapmerge)
 
 
 class FilterMixin(local):

File tests/mixins.py

             ).worker(lambda x, y: x * y).mapping().get(),
             [2, 6, 12, 2, 6, 12],
         )
+        self.assertEqual(
+            self.mclass(
+                dict([(1, 2), (2, 3), (3, 4)]), dict([(1, 2), (2, 3), (3, 4)])
+            ).mapping(mapmerge=True).get(),
+            dict([(1, 2), (2, 3), (3, 4)]),
+        )
 
 
 class Mixin(object):